如何在 PowerShell 字符串文字中编码 32 位 Unicode 字符? [英] How to encode 32-bit Unicode characters in a PowerShell string literal?
问题描述
这个堆栈溢出问题 处理 16 位 Unicode 字符.我想要一个支持 32 位字符的类似解决方案.请参阅此链接以获取各种 Unicode 图表的列表.例如,一系列 32 位字符是 音乐符号.
This Stack Overflow question deals with 16-bit Unicode characters. I would like a similar solution that supports 32-bit characters. See this link for a listing of the various Unicode charts. For example, a range of characters that are 32-bit are the Musical Symbols.
上面链接的问题中的答案不起作用,因为它将 System.Int32 值转换为 System.Char,这是一种 16 位类型.
The answer in the question linked above doesn't work because it casts the System.Int32 value as a System.Char, which is a 16-bit type.
让我澄清一下,我并不特别关心显示 32 位 Unicode 字符,我只想将字符存储在字符串变量中.
Let me clarify that I don't particularly care about displaying the 32-bit Unicode character, I just want to store the character in a string variable.
编辑 #2:我编写了一个 PowerShell 片段,它使用标记答案及其注释中的信息.我本来想把它放在另一个评论中,但评论不能是多行的.
Edit #2: I wrote a PowerShell snippet that uses the info in the marked answer and its comments. I would have wanted to put this in another comment, but comments can't be multi-line.
$inputValue = '1D11E'
$hexValue = [int]"0x$inputValue" - 0x10000
$highSurrogate = [int]($hexValue / 0x400) + 0xD800
$lowSurrogate = $hexValue % 0x400 + 0xDC00
$stringValue = [char]$highSurrogate + [char]$lowSurrogate
Dour High Arch 的答案仍然值得称赞,因为它帮助我最终理解了代理对.
Dour High Arch still deserves credit for the answer for helping me finally understand surrogate pairs.
推荐答案
假设 PowerShell 使用 UTF-16,32 位代码点表示为 代理人.例如,U+10000 表示为:
Assuming PowerShell uses UTF-16, 32-bit code points are represented as surrogates. For example, U+10000 is represented as:
0xD100 0xDC00
即两个16位字符;十六进制 D100 和 DC00.
That is, two 16-bit chars; hex D100 and DC00.
祝你找到带有代理字符的字体.
Good luck finding a font with surrogate chars.
这篇关于如何在 PowerShell 字符串文字中编码 32 位 Unicode 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!