将十六进制字符串转换回char

时间:2011-12-06 12:44:17

标签: c# .net hex

我知道 - 关于这一点有很多话题,但即使我看了很多他们都无法找到解决方案.. 我正在将char转换为十六进制:

char c = i;
int unicode = c;
string hex = string.Format("0x{0:x4}", unicode);

问题:如何将hex转换为char?

2 个答案:

答案 0 :(得分:20)

你可以尝试:

hex = hex.Substring(2); // To remove leading 0x
int num = int.Parse(hex, NumberStyles.AllowHexSpecifier);
char cnum = (char)num;

答案 1 :(得分:3)

using System;
using System.Globalization;

class Sample {
    static void Main(){
        char c = 'あ';
        int unicode = c;
        string hex = string.Format("0x{0:x4}", unicode);
        Console.WriteLine(hex);
        unicode = int.Parse(hex.Substring(2), NumberStyles.HexNumber);
        c = (char)unicode;
        Console.WriteLine(c);
    }
}