c#16进制字符串转ASCII字符串
时间: 2024-01-08 08:02:45 浏览: 172
16进制转字符-字符转16进制-有调用方法-貌似支持中文-但绝对能转
以下是C#中将16进制字符串转换为ASCII字符串的方法:
```csharp
public static string HexStringToASCII(string hexstring)
{
byte[] bt = HexStringToBinary(hexstring);
string lin = "";
for (int i = 0; i < bt.Length; i++)
{
lin = lin + bt[i] + " ";
}
return lin;
}
public static byte[] HexStringToBinary(string hexstring)
{
// 将16进制字符串转换为字节数组
string[] hexValuesSplit = hexstring.Split(' ');
byte[] bytes = new byte[hexValuesSplit.Length];
for (int i = 0; i < hexValuesSplit.Length; i++)
{
bytes[i] = Convert.ToByte(hexValuesSplit[i], 16);
}
return bytes;
}
```
使用示例:
```csharp
string hexString = "61 62 63"; // 16进制字符串
string asciiString = HexStringToASCII(hexString);
Console.WriteLine(asciiString); // 输出:a b c
```
阅读全文