一个byte怎么转成网络字节序

chen_chen1212 2009-11-11 01:23:55
一个byte怎么转成网络字节序
...全文
459 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
chen_chen1212 2009-11-11
  • 打赏
  • 举报
回复
搞定了,谢谢大家
soaringbird 2009-11-11
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 chen_chen1212 的回复:]
这个转换后byte[] btType = BitConverter.GetBytes(stcl.nType); 长度编程2了
[/Quote]
nType是什么类型?
soaringbird 2009-11-11
  • 打赏
  • 举报
回复
应该可以
chen_chen1212 2009-11-11
  • 打赏
  • 举报
回复
这个转换后byte[] btType = BitConverter.GetBytes(stcl.nType); 长度编程2了
jiezi316 2009-11-11
  • 打赏
  • 举报
回复

差不多吧
字符转BYTE数组用 Encoding.Default.GetBytes
chen_chen1212 2009-11-11
  • 打赏
  • 举报
回复
我先转成网络字节序赋值给结构体
byte[] btLen = BitConverter.GetBytes(stcl.nLen);//int long 转byte
byte[] btType = BitConverter.GetBytes(stcl.nType);
byte[] btPVersion = BitConverter.GetBytes(stcl.nPVersion);
byte[] btSocketFd = BitConverter.GetBytes(stcl.nSocketFd);
byte[] btPriority = BitConverter.GetBytes(stcl.bPriority);
//byte[] bSendData = new byte[14];
ArrayList arrTemp = new ArrayList();
arrTemp.AddRange(btLen);
arrTemp.AddRange(btType);
arrTemp.AddRange(btPVersion);
arrTemp.AddRange(btSocketFd);
arrTemp.AddRange(btPriority);
byte[] bSendData = (byte[])arrTemp.ToArray(typeof(byte));
这样对不?
soaringbird 2009-11-11
  • 打赏
  • 举报
回复
比如说,协议规定0,1,2,3字节是nLen,网络字节序,
那就
byte[]buf=new byte[](xxx);
Array.Copy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(nLen)),0,buf,0,4)
以此类推
chen_chen1212 2009-11-11
  • 打赏
  • 举报
回复
给写写呗,怎么填啊
soaringbird 2009-11-11
  • 打赏
  • 举报
回复
嗯,按照协议规定,一个字段一个字段填充
chen_chen1212 2009-11-11
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 soaringbird 的回复:]
上面代码有误,稍等。
字节对齐是在结构定义时指定的。
[/Quote]
不能强制对齐,我需要一个一个考进去吧
soaringbird 2009-11-11
  • 打赏
  • 举报
回复
上面代码有误,稍等。
字节对齐是在结构定义时指定的。
chen_chen1212 2009-11-11
  • 打赏
  • 举报
回复
要自己转成byte的,怎么转啊,我的结构里面有int,有byte,还要防止字节对齐,怎么弄啊
jiezi316 2009-11-11
  • 打赏
  • 举报
回复

貌似数字才转吧。其实也可以不转。转不转关键看你的另一端的情况。
soaringbird 2009-11-11
  • 打赏
  • 举报
回复
如果那个结构是别人写好的,你不能改,那就只能一个字段一个字段处理字节序了。
soaringbird 2009-11-11
  • 打赏
  • 举报
回复
结构定义成这样的,因为你的协议规定了用网络字节序,跟x86的不一样。
struct stClientHead
{
private int _nLen;
public int nLen
{
get{return IPAddress.HostToNetworkOrder(_nLen);}获取时再转回来。
set{_nLen=IPAddress.HostToNetworkOrder(value);} 赋值时转一下
}

fixed(stClientHead *pstcl=&stcl)
{
byte*pb=(byte*)pstcl;
return *pb;
......
*pb就是byte[],你可以把它发送出去。
烈火蜓蜻 2009-11-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 soaringbird 的回复:]
源数据是多字节的(比如int32)才需要转,单纯的字节数组不需要。
其实,转不转要看双方是否一致,一致的话就不用转了
[/Quote]

同意

当存在某个多字节数据的时候,低字节数据在前,高字节在后,而我们正常的数据高位前,低在后,所以才存在要转换的情况,双方可以约定不转。
chen_chen1212 2009-11-11
  • 打赏
  • 举报
回复
不懂啊,copyto不行吗,开辟空间用这个拷贝进去?
soaringbird 2009-11-11
  • 打赏
  • 举报
回复
fixed(stClientHead *pstcl=&stcl)
{
byte*pb=(byte*)pstcl;
return *pb;
......

struct stClientHead
{
private int _nLen;
public int nLen
{
get{return IPAddress.HostToNetworkOrder(_nLen);}
set{_nLen=IPAddress.HostToNetworkOrder(value);}
}

chen_chen1212 2009-11-11
  • 打赏
  • 举报
回复
那边要求发过去是网络字节序,
stClientHead stcl = new stClientHead();
stcl.nLen = 0;
stcl.nType = setbyte;
stcl.nPVersion = 0;
stcl.nSocketFd = 0;
放到一个数组里啊??
soaringbird 2009-11-11
  • 打赏
  • 举报
回复
源数据是多字节的(比如int32)才需要转,单纯的字节数组不需要。
其实,转不转要看双方是否一致,一致的话就不用转了
加载更多回复(7)

110,534

社区成员

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

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

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