如何使用Socket传输uint(System.UInt32)类型的数字?

JeasonZhao 2006-04-16 02:56:56
一个命令字为0x80000001,需要使用Socket传送给 Unix机器
使用 System.Net.IPAddress.HostToNetworkOrder 中居然没有传递uint的类型。
怎么传输啊??
int型可以使用
int nNew=System.Net.IPAddress.HostToNetworkOrder(n);
socket.Send(BitConverter.GetBytes(nNew));


uint怎么办,达人解释一下
...全文
227 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
JeasonZhao 2006-04-17
  • 打赏
  • 举报
回复
谢谢,我先试试看能不能搞定
Knight94 2006-04-16
  • 打赏
  • 举报
回复
用4个字节进行传递,例如:

private byte[] UIntToBytes( uint nValue )
{
byte[] bArray = new byte[4];
byte[0] = (byte)(nValue & 0xFF);
byte[1] = (byte)( ( nValue >> 8 ) & 0xFF );
byte[2] = (byte)( ( nValue >> 16 ) & 0xFF );
byte[3] = (byte)( ( nValue >> 24 ) & 0xFF );
return bArray;
}

private uint BytesToUInt ( byte[] bArray )
{
if( bArray == null || bArray.Length != 4 )
//Throw exception here
else
{
uint nValue = 0;
nValue ^= byte[3];
nValue = nValue << 8;
nValue ^= byte[2];
nValue = nValue << 8;
nValue ^= byte[1];
nValue = nValue << 8;
nValue ^= byte[0];
return nValue;
}
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧