我正在尝试将两个字节转换为一个无符号的短信,以便我可以检索实际的服务器端口值。我从这个
protocol specification的回复格式基础上。我试过使用
BitConverter.ToUint16()这个,但问题是,它似乎没有抛出预期的价值。见下面的示例实现:
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;
}
给出一个样本数据,假设两个字节值,我有105& 135,转换后的预期端口值应为27015,而是使用BitConverter获取值34665。
我做错了吗?