c# 16进制大端小端解析长度

//前两个字节为长度的解析
string hexstr = "00 13 59 02 80 00 E7 00 80 00 E9 00 80 00 EA 00 80 00 EB 00 80";
byte[] hexarr= strToToHexByte(hexstr); byte[] countarray = new byte[];
Array.Copy(hexarr, , countarray, , ); short count = (short)((countarray[] << ) + countarray[]);
byte[] bytearr = new byte[count];
Array.Copy(hexarr, , bytearr, , count);
string t = BitConverter.ToString(bytearr);
Console.WriteLine(t); private static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % ) != )
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / ];
for (int i = ; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * , ), );
return returnBytes;
}
//指定转换
int a = int.Parse("", NumberStyles.HexNumber);
//判断大端小端,vs默认小端
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(b); // 反转
}
BitConverter.ToString(array[]);
十六进制为 1A2B3C5E
小端输出 为 5E-3C-2B-1A
大端输出 为 1A-2B-3C-5E
上一篇:jQuery 开发一个简易插件


下一篇:(转)C系程序员面试必知必会之大端小端