Conversion to Uint16 fails

jnpir

Member
Joined
Apr 14, 2007
Messages
14
Programming Experience
10+
I have some C# code:

private ushort SourcePort;

SourcePort = (
ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());

Since VB has no ushort type I can use the System.Uint16

Private SourcePort as System.Uint16

SourcePort = Convert.ToUInt16(IPAddress.NetworkToHostOrder(binaryReader.ReadInt16))

A value of -5386 converted to ushort in the above C# is 60151. Thats not larger than the System.Uint16 maximum value. When I convert the same -5386 using the above VB code I get an overflow exception telling me the value is either larger or smaller than what a Uint16 can handle. A Uint16 maximum is 65535

Anyone have some ideas?
 
Why dont you just work with byte arrays? I dont think I ever did network access thinking "i want to read 4 bytes, I'll read a UInt32"

Additionally, what do we care if it's signed or not? signed runs from 0 -> 127 -> -128 -> -1, unsigned runs from 0 -> 255 but its still the same binary? 1111 1111 is 255, or -1 but who cares of the value? Both will still hex to 0xFF, and both will still be sent down a net stream as 1111 1111?
 
Back
Top