1.整数与字节互转: BitConverter.GetBytes(),BitConverter.ToUInt16()
int a = 57847;
byte[] textByte = BitConverter.GetBytes(a);
//C#中字节是由低位向高位排列,所以57847=E1F7H转化为byte[]时是{ 0xF7,0xE1}
byte[] textByte = { 0xF7,0xE1};
int a = BitConverter.ToUInt16(textByte, 0);
2. 16进制ASCII码转换为字符串
byte[] textByte = { { 0x31,0x32}}; // ASCII码0x31,0x32对应字符为“1”,“2”
string textString = Encoding.ASCII.GetString(textByte); //结果为“12”
3. 2进制位置零或置1,转换为整数
int a = 57847;
byte[] textByte = BitConverter.GetBytes(a);
byte[] textByte1 = new byte[] { textByte[0], textByte[1] };
BitArray bit = new BitArray(textByte1);
// 57847D=E1F7H=1110 0001 1111 0111 B
bit[10] = true; //第10位置1 → 1110 0101 1111 0111 B
int res = 0;