【发布时间】:2014-01-19 09:33:11
【问题描述】:
我已阅读 a question about UTF-8, UTF-16 and UCS-2,几乎所有答案都表明 UCS-2 已过时,C# 使用 UTF-16。
但是,我在 C# 中创建 4 字节字符 U+1D11E 的所有尝试都失败了,所以我实际上认为 C# 仅使用 UTF-16 的 UCS-2 子集。
有我的尝试:
string s = "\u1D11E"; // gives the 2 character string "ᴑE", because \u1D11 is ᴑ
string s = (char) 0x1D11E; // won't compile because of an overflow
string s = Encoding.Unicode.GetString(new byte[] {0xD8, 0x34, 0xDD, 0x1E}); // gives 㓘ờ
C# 字符串真的是 UTF-16 还是真的是 UCS-2?如果它们是 UTF-16,我如何将小提琴谱号放入我的 C# 字符串中?
【问题讨论】:
-
最简单的就是在源代码中包含字符,即
string s = "????";。我建议您使用 UTF-8 编码保存您的.cs文件。 Supplementary Multilingual Plane 中的这个字符将占用 UTF-8 中的四个八位字节。当保存在内存中时,它将占用两个 UTF-16 代码单元或char值,即所谓的代理对。 -
是的,我在 Wikipedia 上读到过,这就是我尝试 Encoding.GetString() 方法的原因。
标签: c# unicode encoding character-encoding utf-16