Re: BitConverter.ToInt16 doesn't work correct
- From: "Michael C" <mike@xxxxxxxxxx>
- Date: Mon, 15 Oct 2007 15:05:54 +1000
"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.
.
- Prev by Date: Re: XML to SQL in Orcas Beta 2
- Next by Date: Re: Getting at items in arraylist
- Previous by thread: Re: BitConverter.ToInt16 doesn't work correct
- Next by thread: Re: Work with bits
- Index(es):
Relevant Pages
|