zoukankan      html  css  js  c++  java
  • c# byte数组各种操作

    1、网络字节序转换

                float m = 5f;
                var btValue = BitConverter.GetBytes(m).Reverse().ToArray();
    

      2、byte数组合并

                byte[] data = new byte[10];
                byte[] counts =new byte[3];
                byte[] ndata = new byte[data.Length + counts.Length];
                data.CopyTo(ndata, 0);
                counts.CopyTo(ndata, data.Length);
    

      3、string和byte[]转换

                string转byte[]:
                byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
                
                byte[]转string:
                string str = System.Text.Encoding.Default.GetString ( byteArray );
                
                string转ASCII byte[]:
                byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str );
                
                ASCII byte[]转string:
                string str = System.Text.Encoding.ASCII.GetString ( byteArray );
    

      4、字符串分割成数组

    string[] b = a.Split('|');
    

      5、int转换成16进制字节

                int a = 58;
                byte b = Convert.ToByte(a);
    

      6、byte[]截取

     byte[] test = buffer.Skip(24).Take(16).ToArray();//从第24位开始截取长度16
    

      7、定义一个list 添加后 转换成byte[]

    List<byte> frameBytes = new List<byte>();
    frameBytes.Add(0x9E);	
    byte[] phoneNumByte=new byte[]{0x01,0x03,0x05,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00};//定义一个数组			
    for (int i = 0; i < phoneNumByte.Length; i++)
     {
       frameBytes.Add(phoneNumByte[i]);
     }
    frameBytes = frameBytes.Concat(dataBody).ToList<byte>();//两个list合并
    byte[] transByte = frameBytes.ToArray();//list转byte[]
    // List<byte> b=transByte.ToList();//byte[]转list
    

      

  • 相关阅读:
    14-3 SQL Server基本操作
    14-2 SQL语言简介
    14-1数据库基础--数据库相关技术
    2.9_Database Interface ADO结构组成及连接方式实例
    2.8_Database Interface ADO由来
    2.7_Database Interface OLE-DB诞生
    容器化技术之K8S
    容器化技术之Docker
    NLP(二)
    cmake
  • 原文地址:https://www.cnblogs.com/webttt/p/12854806.html
Copyright © 2011-2022 走看看