zoukankan      html  css  js  c++  java
  • 按照指定的格式解析字节数组

    //按照指定的格式解析字节数组
        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; }
        }
  • 相关阅读:
    整理SVN代码-->正式环境的代码
    业务(1)
    在一个项目中跨领域调用接口的的实现
    一个java文件编译之后会产生多个class文件
    shutil模块
    shevle模块
    confiparser模块
    sys模块
    subprocess模块
    【ADO.NET】3、从TXT中导入数据到数据库
  • 原文地址:https://www.cnblogs.com/mahuanpeng/p/6866169.html
Copyright © 2011-2022 走看看