天天看點

資料之間的轉換

1、string轉byte數組【"<02GS0000FF>"轉十六進制3C 30 32 47 53 30 30 30 30 46 46 3E】

string msg = "<02GS0000FF>";

byte[] buffer = System.Text.Encoding.ASCII.GetBytes(msg);

2、數字與byte[]之間的轉換【100轉byte[0x64]】

using System;

int  i = 123;

byte [] intBuff = BitConverter.GetBytes(i);     // 将 int 轉換成位元組數組

lob.Write(intBuff, 0, 4);

i = BitConverter.ToInt32(intBuff, 0);           // 從位元組數組轉換成 int

double x = 123.456;

byte [] doubleBuff = BitConverter.GetBytes(x);  // 将 double 轉換成位元組數組

lob.Write(doubleBuff, 0, 8);

x = BitConverter.ToDouble(doubleBuff, 0);       // 從位元組數組轉換成 double

3、數字轉成帶占位符的string【100轉"64"】

string hex = string.Format("{0:X2}", 100);//hex=64

長風破浪會有時,直挂雲帆濟滄海!

可通過下方連結找到部落客

https://www.cnblogs.com/judes/p/10875138.html