Re: BitConverter.ToInt16 doesn't work correct



"SushiSean" <SushiSean@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:12713D70-6F57-4DA6-8ECF-E4694236C356@xxxxxxxxxxxxxxxx
Hello. I have a problem with getting short value from 2 byte array.
I have this code. There are 2 short values in bytes.

byte[] cast = { 18, 152, 00, 80 };
Int32 port = BitConverter.ToInt16(cast, 0); // -26606 - wrong!
Int32 port2 = BitConverter.ToInt16(cast, 2); // 20480 - wrong!

//port and port2 get not correct values
//and I found solution in google
short result1 = (short)(((short)cast[1]) | (short)(cast[0] * 256));
//4760 -
correct!
short result2 = (short)(((short)cast[3]) | (short)(cast[2] * 256)); //80 -
correct!

why it happends? How make BitConverter work correct?

You could just do cast[0] * 256 + cast[1]. You will need to work out how to
handle values of 128 or above for the first byte.


.



Relevant Pages

  • Re: Computers got Hacked?? Please Help!!!
    ... something strange has happened to our work computers. ... > google and etc. ... This happends randomly throughout the day. ...
    (microsoft.public.security)
  • Computers got Hacked?? Please Help!!!
    ... something strange has happened to our work computers. ... happened to our windows 2003 server and several other users' pcs (win2K and ... This happends randomly throughout the day. ... Searched on google for similar incident, ...
    (microsoft.public.security)