1、大小端转换
//
//U32类型数据转换为byte[]
//高位在前
public static byte[] U32ToBytes(UInt32 x)
{
byte []src=new byte[4];
src[0] = (byte)((x >> 24) & 0xff);
src[1] = (byte)((x >> 16) & 0xff);
src[2] = (byte)((x >> 8) & 0xff);
src[3] = (byte)((x >> 0) & 0xff);
return src;
}
//
//byte[]数据转换为U32类型
//高位在前
public static UInt32 BytesToU32(byte[] x)
{
UInt32 u = 0;
for (int i = 0; i < 4; i++)
{
u += (UInt32)(x[i] << (24 - i * 8));
}
return u;
}
//小端
//U32类型数据转换为U32类型数据
//返回大端
public static UInt32 BigEndFromLED(UInt32 x)
{
byte[] a = BitConverter.GetBytes(x);
Array.Reverse(a);
return BitConverter.ToUInt32(a, 0);
}