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