c语言将short转为2个byte,在C#中将2个字节转换为Short

我正在尝试将两个字节转换为一个无符号的短信,以便我可以检索实际的服务器端口值。我从这个

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。

我做错了吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值