怎样将int类型转换为16进制的byte数组

wei023 2010-11-04 02:14:56
要进行UDP通信,现在要将一段字符串的长度写进byte数组
例:
如果输入的字符串长度为15

byte[] sendDataLength = new byte[2];
结果就是
sendDataLength[0] = 0x00;
sendDataLength[1] = 0x0F;

请问想得到这样的结果代码应该怎么写,先谢谢了
...全文
327 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gomoku 2010-11-04
  • 打赏
  • 举报
回复

byte[] sendDataLength = BitConverter.GetBytes( (short)15 );
Array.Reverse(sendDataLength ); // change to bigendian
gomoku 2010-11-04
  • 打赏
  • 举报
回复
[code=C#
]byte[] sendDataLength = BitConverter.GetBytes( (short)15 );
Array.Reverse(sendDataLength ); // change to bigendian
[/code]

111,129

社区成员

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

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

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