byte數組操作


//1.字節轉換
float m = 5f;
var btValue = BitConverter.GetBytes(m).Reverse().ToArray();
//轉為原值字符串
string m1 = System.Text.Encoding.Default.GetString(btValue);

//2.byte 數組合並
byte[] data = new byte[10];
byte[] counts = new byte[3];
byte[] ndata = new byte[data.Length + counts.Length];
//將data復制到ndata
data.CopyTo(ndata, 0);//從ndata的下標為0的地方開始存放
counts.CopyTo(ndata, data.Length);

//3.string和byte[]轉換
string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//string轉byte[]:
byte[] byteArray1 = System.Text.Encoding.Default.GetBytes(str);
//byte[] 轉string:
string str1 = System.Text.Encoding.Default.GetString(byteArray1);
//string轉ASCII byte[]:
byte[] byteArray2 = System.Text.Encoding.ASCII.GetBytes(str);
//ASCII byte[] 轉string:
string str2 = System.Text.Encoding.ASCII.GetString(byteArray2);

//4.字符串拆分數組
string a = "A|B|C|D";
string[] a1 = a.Split('|');

//5.Int轉為16進制
int b = 58;
byte b1 = Convert.ToByte(b);

//6.byte數組截取
byte[] test = byteArray2.Skip(4).Take(3).ToArray();//從下標4開始截取長度3

//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(byteArray2).ToList<byte>();//兩個list合並
//list轉byte[]
byte[] transByte = frameBytes.ToArray();
//byte[]轉list
List<byte> lb =transByte.ToList();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM