views:

50

answers:

2

hello I'm trying to convert an int value to 16 bit unsigned char type (USHORT) in example 41104 is A909 in ushort

but in c# i tried code samples as

byte[] bytes = BitConverter.GetBytes(41104);
string bytes = BitConverter.ToString(byteArray);
//it reurns "90-A0"

(with the http://msdn.microsoft.com/tr-tr/library/8wwsdz3k.aspx msnd help)

so how to get A909 value as ushort for 41104 ?

thnx

+2  A: 

Actually the correct (=hexadecimal) value is A090. I doesn't matter whether it's ushort or not, what you want is to display the hexadecimal equivalent.

The code snippet you provided already does this. One byte is "A0" the other one "90". You just have to get the order right.

Another way is to let .NET do the job

String.Format("{0:X}", 41104);

As you can see it's not really a data conversion, but rather a different way of display.

also some values have to convert to ULONG,some values have to convert USHORT and somes should convert to SHORT data type how to convert these types ??thnx
dankyy1
i'm googling and found as converting was...string.Format("{0:X16}", 41104) for ushortstring.Format("{0:X32}", 41104) for ulongstring.Format("{0:X8}", 41104) for bytecan be used
dankyy1
+1  A: 

do you need to reorder the bytes?

byte[] bytes = BitConverter.GetBytes(41104);
if (BitConverter.IsLittleEndian)
{    List<byte> tmp = new List<byte>();
     tmp.AddRange(bytes);
     tmp.Reverse();
     bytes = tmp.ToArray();
}
Arseny
thnx for your reply .in the code sample above () is forgetten after tmp is defined so code block should be as on 3rd lineList<byte> tmp = new List<byte>();
dankyy1