如何:将字符串中的 unicode 字符表示转换为实际的 unicode 字符 [英] HOWTO : convert unicode character representation in string to the actual unicode character

查看:46
本文介绍了如何:将字符串中的 unicode 字符表示转换为实际的 unicode 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using font awesome in a Xamarin application. The api to which the XamarinApp is talking returns a fxxx string to indicate which icon to show. In code I add \u but it is seen as a string and not a unicode character.

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

我尝试了 char.Parse 但它抛出了一个错误:

I tried char.Parse but it throws an error:

System.FormatException: 字符串必须正好是一个字符长.

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

有什么建议吗?

推荐答案

你想要一个 Char 保存私有代码点的值 U+F641.

You want to have a Char holding the value of the private-use code point U+F641.

您可以通过将其解析为它所代表的十六进制值来实现:

You can do so by parsing it as the hexadecimal value it represents:

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

然后将其转换为Char:

char c = (char)p;

根据可能的代码点范围,char 中可能没有足够的空间来存储代码点,如@Panagiotis 所示,请使用 Char.ConvertFromUtf32(int):

Depending on the range of possible code points, you may not have enough space in a char to store the code point, so as @Panagiotis indicates, use Char.ConvertFromUtf32(int):

string chars = Char.ConvertFromUtf32(p);

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

But then you'll have a string, not a single char.

这篇关于如何:将字符串中的 unicode 字符表示转换为实际的 unicode 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆