Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting 2 bytes to Short in C#

I'm trying to convert two bytes into an unsigned short so I can retrieve the actual server port value. I'm basing it off from this protocol specification under Reply Format. I tried using BitConverter.ToUint16() for this, but the problem is, it doesn't seem to throw the expected value. See below for a sample implementation:

int bytesRead = 0;  while (bytesRead < ms.Length) {     int first = ms.ReadByte() & 0xFF;     int second = ms.ReadByte() & 0xFF;     int third = ms.ReadByte() & 0xFF;     int fourth = ms.ReadByte() & 0xFF;     int port1 = ms.ReadByte();     int port2 = ms.ReadByte();     int actualPort = BitConverter.ToUInt16(new byte[2] {(byte)port1 , (byte)port2 }, 0);     string ip = String.Format("{0}.{1}.{2}.{3}:{4}-{5} = {6}", first, second, third, fourth, port1, port2, actualPort);     Debug.WriteLine(ip);     bytesRead += 6; } 

Given one sample data, let's say for the two byte values, I have 105 & 135, the expected port value after conversion should be 27015, but instead I get a value of 34665 using BitConverter.

Am I doing it the wrong way?

like image 999
Rafael Ibasco Avatar asked Apr 21 '11 21:04

Rafael Ibasco


People also ask

Is a short always 2 bytes?

The size of the short type is 2 bytes (16 bits) and, accordingly, it allows expressing the range of values equal to 2 to the power 16: 2^16 = 65 536.

How to convert int to bytes in c++?

We split the input integer (5000) into each byte by using the >> operator. The second operand represents the lowest bit index for each byte in the array. To obtain the 8 least significant bits for each byte, we & the result with 0xFF . Finally, we print each byte using the print function.

Can we convert byte to short in Java?

The shortValue() method of Byte class is a built in method in Java which is used to return the value of this Byte object as short. Return Value: It returns the value of ByteObject as short.


1 Answers

If you reverse the values in the BitConverter call, you should get the expected result:

int actualPort = BitConverter.ToUInt16(new byte[2] {(byte)port2 , (byte)port1 }, 0); 

On a little-endian architecture, the low order byte needs to be second in the array. And as lasseespeholt points out in the comments, you would need to reverse the order on a big-endian architecture. That could be checked with the BitConverter.IsLittleEndian property. Or it might be a better solution overall to use IPAddress.HostToNetworkOrder (convert the value first and then call that method to put the bytes in the correct order regardless of the endianness).

like image 192
Mark Wilkins Avatar answered Sep 20 '22 21:09

Mark Wilkins