BitConverter.ToInt16 Issue

  • Thread starter Thread starter Michael Davidov
  • Start date Start date
M

Michael Davidov

byte []array = {0x00,0x04};
short ashort = BitConverter.ToInt16(array,0);
Console.WriteLine(ashort.ToString());


Why am I wrong to think that this should return a 4 instead of 1024?
Also, is there a way to get a short value of 4 from those 2 bytes
without switching their possition in the byte array?


Thanks for your input,
Michael Davidov
 
Michael Davidov said:
byte []array = {0x00,0x04};
short ashort = BitConverter.ToInt16(array,0);
Console.WriteLine(ashort.ToString());

Why am I wrong to think that this should return a 4 instead of 1024?

Because BitConverter is little-endian in the MS implementation.
Also, is there a way to get a short value of 4 from those 2 bytes
without switching their possition in the byte array?

Yup - use a class which lets you deal with it in a big-endian way.
Fortunately, I have such a class you can use :)

Look at EndianBitConverter in my MiscUtil library
http://www.pobox.com/~skeet/csharp/miscutil
 
Back
Top