//按照指定的格式解析字节数组
class Program
{
static void Main(string[] args)
{
//格式要求:第1个字节:表示数据类型;第2个字节:表示终端编号长度;第5个字节:表示车辆编号长度
byte[] recBuffer = new byte[8] { 0x01, 0x02, 0x62, 0x63, 0x03, 0x64, 0x65, 0x66 };
MemoryStream stream = new MemoryStream(recBuffer);
int value = stream.ReadByte();
if (value != -1)
{
Type1 type1 = (Type1)value;
value = stream.ReadByte();
if (value != -1)
{
byte terminalCodeLength = (byte)value;
byte[] terminalCodeBytes = new byte[terminalCodeLength];
value = stream.Read(terminalCodeBytes, 0, terminalCodeLength);
string terminalCode = Encoding.UTF8.GetString(terminalCodeBytes);
if (value != -1)
{
value = stream.ReadByte();
byte carCodeLength = (byte)value;
byte[] carCodeBytes = new byte[carCodeLength];
value = stream.Read(carCodeBytes, 0, carCodeLength);
string carCode = Encoding.UTF8.GetString(carCodeBytes);
ClassA classA = new ClassA(type1, terminalCode, carCode);
}
}
}
stream.Close();
stream.Dispose();
}
}
public enum Type1 : byte
{
GPS = 0x01,
CAN = 0x02,
Data = 0x03
}
public class ClassA
{
public ClassA() { }
public ClassA(Type1 type, string terminalCode, string carCode)
{
this.Type = type;
this.TerminalCode = terminalCode;
this.CarCode = carCode;
}
public Type1 Type { get; set; }
public string TerminalCode { get; set; }
public string CarCode { get; set; }
}