BitConverter.ToUInt16

MrLucky

Well-known member
Joined
Mar 5, 2006
Messages
47
Ive found something very strange.

Based on this information:
Code:
Reply Format

The reply always starts with FF FF FF FF 66 0A.

The format is then a series of these server address blocks:
Type 	Data
Byte 	First octet of IP address
Byte 	Second octet of IP address
Byte 	Third octect of IP address
Byte 	Fourth octet of IP address
[b]Unsigned Short 	Port number - usually 27015 (69 87) - this is network ordered, which is unlike every other Steam protocol.[/b]

Some of the servers may be unreachable, so query each server directly to find out. Note also that this list is not exhaustive, so if youre looking for a particular type of server make sure that you specify a filter with the query, rather than filtering client side.

http://developer.valvesoftware.com/wiki/Master_Server_Query_Protocol

But when I do this:
C#:
System.Windows.Forms.MessageBox.Show(BitConverter.ToUInt16(new byte[] { 0x69, 0x87 }, 0).ToString());

I get as result this:

And as the information said, 69 87 should be 27015 (which is the default port for steam servers).

Am I using the wrong function for a Unsigned short or is an other problem?

Fixed, I used IPAddress.HostToNetworkOrder() :)
 
Last edited by a moderator:
Just to clarify, you are using the wrong byte order. When you write a hex number, you write in most signifigant byte first order, whereas (I believe) in the computers internal representation (which is what bit converter deals with) numbers are represented as least signifigant byte first, which means that {0x69, 0x87} would actually represent the hex value 0x8769, or 34665.
 
That requires going via a string, to avoid that step you can just move the high byte into position in a new short, and Or with the low byte:
Code:
Dim bytes As Byte() = {&H69, &H87}
 little endian conversion, big endian byte order:
Console.WriteLine(BitConverter.IsLittleEndian)
Console.WriteLine(BitConverter.ToUInt16(bytes, 0))
 Bitconverter is fine, if you reverse the byte order
 (could use array.reverse) 
Console.WriteLine(BitConverter.ToUInt16(New Byte() {bytes(1), bytes(0)}, 0)) 
 or manipulate the bits.
Console.WriteLine((CType((bytes(0)), UInt16) << 8) Or bytes(1))
C#:
byte[] bytes = {0x69, 0x87};
Console.WriteLine(BitConverter.IsLittleEndian);
Console.WriteLine(BitConverter.ToUInt16(bytes, 0));
Console.WriteLine(BitConverter.ToUInt16(new byte[] {bytes[1], bytes[0]}, 0));
Console.WriteLine(((UInt16)(bytes[0] << 8) | bytes[1]));

or you could use:
System.Net.IPAddress.NetworkToHostOrder
to switch the endian-ness, but only for int16, int32, int64
 
Last edited by a moderator:

Similar threads

P
Replies
0
Views
176
Policy standard local admin account with Active Di
P
Back
Top