首页 > 解决方案 > 如何将 unicode 集从 db 转换为字符?

问题描述

我需要将从数据库字段中获取的 unicode 字符转换为字符串值。在数据库字段中,unicode 字符的格式为 U+0024,接下来我得到 \u0024 格式,但我无法转换它。

string a = "U+0024";
string b = a.Remove(0, 2);
string c = @"\u" + b;
string d = System.Uri.UnescapeDataString(c);
Console.WriteLine(d);
// There is \u0024 in output
string e =System.Uri.UnescapeDataString(\u0024);
Console.WriteLine(e);
//There is $ in output that I would like to

标签: c#

解决方案


您从数据库中获得的字符串似乎是 Unicode 代码点,因为它们的格式为U+XXXX.

有一个非常有用的方法称为char.ConvertFromUtf32将 Unicode 代码点转换为包含单个char或 s 代理对的字符串char

此方法接受intas 参数,因此您需要将b字符串(十六进制)转换为int.

int codepoint = Convert.ToInt32(b, 16);

然后,将其传递给ConvertFromUtf32

string result = char.ConvertFromUtf32(codepoint);

推荐阅读