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[2]; Array.Copy(hexarr, 0, countarray, 0, 2); short count = (short)((countarray[0] << 8) + countarray[1]); byte[] bytearr = new byte[count]; Array.Copy(hexarr, 2, bytearr, 0, count); string t = BitConverter.ToString(bytearr); Console.WriteLine(t); private static byte[] strToToHexByte(string hexString) { hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString += " "; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return returnBytes; }
//指定转换
int a = int.Parse("0013", NumberStyles.HexNumber);
//判断大端小端,vs默认小端
if (!BitConverter.IsLittleEndian)
{
       Array.Reverse(b); // 反转
}
BitConverter.ToString(array[]);
十六进制为 1A2B3C5E
小端输出 为 5E-3C-2B-1A
大端输出 为 1A-2B-3C-5E
原文地址:https://www.cnblogs.com/smartsensor/p/8610243.html