字符char
是two bytes值,这就是为什么它只能在[\u0000..\uFFFF]
范围内.
如果您要查找值beyond \uFFFF
,请注意,它应该表示为two characters struct ,例如string
:
string result = "\U000103BD"; // note Capital U, 8 hex digits
Console.Write(result);
Output:个个个
????
如果您想显式使用这两个字符,可以使用下面的解码:
public static (char left, char right) Decode(int code) {
if (code < 0x10000)
throw new ArgumentOutOfRangeException(nameof(code));
return (
(char)(((code - 0x10000) >> 10) + 0xD800),
(char)(((code - 0x10000) & 0b1111_111_111) + 0xDC00)
);
}
Demo:个
(char left, char right) = Decode(0x103BD);
Console.Write(string.Concat(left, right));
Output:个个个
????
最后,从.Net 6开始,您可以使用Rune struct 来处理这些字符对:
Rune rune = new Rune(0x103BD);
Console.Write(rune);
Output:个个个
????