c# - 如何 : convert unicode character representation in string to the actual unicode character

标签 c# unicode xamarin.forms font-awesome

我在 Xamarin 应用程序中使用了很棒的字体。 XamarinApp 正在与之交谈的 api 返回一个 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)代码点 U+F641 的值.

您可以通过将其解析为它表示的十六进制值来做到这一点:

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

然后将其转换为 Char :
char c = (char)p;

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

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

关于c# - 如何 : convert unicode character representation in string to the actual unicode character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56018380/

相关文章:

c# - Xamarin.forms - 如何在 MapView 上创建可拖动元素(标记)?

c# - 如何以编程方式在 C# 中获取 SVN 修订说明和作者?

C# Entity Framework 6 MySQL - 插入 ApplicationUser_Id 时引用的未知列

c# - 将键/值对绑定(bind)到 asp 标签的最佳方法是什么

python - 将 unicode 保存到数据库

unicode - 在批处理文件中使用方框图 Unicode 字符

ios - iOS 上的 ForceUpdateSize ListView 问题

c# - 更改 Xamarin.Forms 中背景图像的纵横比,同时保持内容居中

c# - 如何在 C# 中以编程方式查找项目文件夹

php - 使用 PHP 和 MySQL 存储和显示 unicode 字符串 (हिन्दी)