C# HEX转ASCII
时间: 2023-11-22 10:46:56 浏览: 99
HEX(16进制)转ASCII,除法保留2位小数
C#中可以使用Encoding.ASCII.GetString()方法将HEX转换为ASCII。具体实现方法如下:
```
byte[] hexBytes = { 0x41, 0x42 }; // 16进制的AB
string asciiString = Encoding.ASCII.GetString(hexBytes); // 转换为字符串"AB"
```
另外,如果需要将10进制整数转换为byte数组,可以使用以下代码:
```
public static byte[] intToBytes(int hexint) {
string HexString = Convert.ToString(hexint, 16);
HexString = HexString.Replace(" ", "");
if ((HexString.Length % 2) != 0) HexString = "0" + HexString;
byte[] returnBytes = new byte[HexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++) {
returnBytes[i] = Convert.ToByte(HexString.Substring(i * 2, 2).Trim(), 16);
}
return returnBytes;
}
```
阅读全文