c# - 如何将 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
解决方案
您从数据库中获得的字符串似乎是 Unicode 代码点,因为它们的格式为U+XXXX
.
有一个非常有用的方法称为char.ConvertFromUtf32
将 Unicode 代码点转换为包含单个char
或 s 代理对的字符串char
。
此方法接受int
as 参数,因此您需要将b
字符串(十六进制)转换为int
.
int codepoint = Convert.ToInt32(b, 16);
然后,将其传递给ConvertFromUtf32
:
string result = char.ConvertFromUtf32(codepoint);
推荐阅读
- mysql - 您可以在一个表中定义一个外键,从该表中派生出具有主键的表吗?
- reactjs - 在 Redux 中存储大数据好吗?
- python - 在 Python 中编辑文件并将结果写入新文件,但仅限于特定区域
- javascript - 检查字符串格式的日期时间是否等于中午
- ios - 在 SwiftUI 中重新关注第一响应者
- oracle - 未使用 Range 区间分区表中的本地索引
- flutter - 如何在 Flutter 的 DropDown 中显示 json 嵌套数组?
- postgresql - 如何在 postgres 中成功显示通知
- c# - c#中用于CATIA脚本的代码行的含义?
- ruby - 错误:ActionView::MissingTemplate(迁移到 Rails 5 后缺少模板应用程序/索引