如何:将字符串中的 unicode 字符表示转换为实际的 unicode 字符

HOWTO : convert unicode character representation in string to the actual unicode character

我在 Xamarin 应用程序中使用了很棒的字体。 XamarinApp 与之通信的 api returns 一个 fxxx 字符串,用于指示要显示的图标。在代码中我添加了 \u 但它被视为字符串而不是 unicode 字符。

    var value = "f641";
    newLabel.Text = char.Parse($"\u{value}").ToString();

我试过 char.Parse 但它抛出错误:

System.FormatException: String must be exactly one character long.

有什么建议吗?

您想要 Char 保存 private-use 代码点 U+F641 的值。

您可以将其解析为它代表的十六进制值:

var input = "f641";
int p = int.Parse(input, System.Globalization.NumberStyles.HexNumber); // 63041

然后将其转换为Char:

char c = (char)p;

根据可能的代码点范围,您可能没有足够的 space 在 char 中存储代码点,因此@Panagiotis 指出,使用 Char.ConvertFromUtf32(int)

string chars = Char.ConvertFromUtf32(p);

但是你会得到一个字符串,而不是一个字符。