一个ushort的数据,怎么转成byte[]?
转成了byte[]后 怎么转回来?我的代码
//测试的testNum1
ushort testNum1=34212;
//测试的testByte
byte[] testByte = new byte[2]; //将testNum1拆开放进包里面
testByte[0] = (byte)((0xff00 & testNum1) >> 8);
testByte[1] = (byte)(0xff & testNum1);
//在网上看到的这一句,不知道有何意义,请知道的朋友说明一下
Array.Reverse(testByte); //将testByte 转回成ushort
ushort test = BitConverter.ToUInt16(testByte, 0);
转成了byte[]后 怎么转回来?我的代码
//测试的testNum1
ushort testNum1=34212;
//测试的testByte
byte[] testByte = new byte[2]; //将testNum1拆开放进包里面
testByte[0] = (byte)((0xff00 & testNum1) >> 8);
testByte[1] = (byte)(0xff & testNum1);
//在网上看到的这一句,不知道有何意义,请知道的朋友说明一下
Array.Reverse(testByte); //将testByte 转回成ushort
ushort test = BitConverter.ToUInt16(testByte, 0);
数字转换为字节数组...
byte[] ba = BitConverter.GetBytes(s);
但是 我这个byte[]数组在我要用的地方,前面是有其他的东西的。这个ushort要放在byte[]数组的 第5~6位之间。
不用移位的话,那我还得创建一个临时的数组,把ushort的数据丢到临时的数组里面之后,再来放到这个总的byte[]数组的 第5~6位之间,也比较麻烦的说。
为什么一定要用Array.Reverse 这个byte[]还原出来的 ushort才是对的......
指针最好用.
所以你的项目没啥特殊要求.
直接unsafe吧.
自己权衡下吧.
{
ushort * put=(ushort *) ptr;
*put //即 ;
}
//将testNum1 转为数组testByte
testByte = BitConverter.GetBytes(CRC16Num); //将testByte转回到ushort
ushort test = BitConverter.ToUInt16(testByte, 0);这样写 倒是对的,反而加了Array.Reverse就有问题了。
最后一问,前面问题基本解决。 //将testNum1 转为数组testByte
testByte = BitConverter.GetBytes(CRC16Num);如何来区别这个testByte 数组里面的高位和低位? CRC16Num=15387;
//将testNum1 转为数组testByte
testByte = BitConverter.GetBytes(CRC16Num);testByte[0] 为27
testByte[1] 为60
哪个是低位?
哪个是高位?新手一个,还望大侠指点迷津!