在PowerShell中显示Unicode Emoji

10
我希望能够在PowerShell中显示Unicode表情符号,例如U+1F4A9。我知道这只能在ISE控制台中使用,但我不知道怎么做。
我尝试过以下方法:
$CharBytes = [System.Text.Encoding]::Unicode.GetBytes("")

这将返回61、216、169、220。但这可能不是0x1F4A9的表示方式吗?
当我尝试时:
[BitConverter]::GetBytes(0x1F4A9)

我将得到一组不同的字节:169、244、1、0。将它们转换为Unicode字符会得到一个。
因此,我的问题是:如何在PowerShell ISE(以及控制台窗口中)显示任何Unicode字符?
3个回答

5

好的,这很简单:我需要使用UTF32而不是Unicode:

$CharBytes = 169, 244, 1, 0
[System.Text.Encoding]::UTF32.GetString($CharBytes)

3

请问您能否提供更多关于您回答的参考资料?例如,在PowerShell中u{}代表什么意思? - krave
以上,“`u{}”是一种通过Powershell解释Unicode字符的转义方式。这是Powershell本身内置的功能。参见:https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.1#unicode-character-ux - MrHockeyMonkey

3

这里有另一种演示高代理项和低代理项的方法,因为码点超过了16位。结果实际上是一个由两个字符组成的字符串。如果您尝试单独显示每个字符,则会显示乱码。

$S = 0x1f600
$S = $S - 0x10000
$H = 0xD800 + ($S -shr 10)
$L = 0xDC00 + ($S -band 0x3FF)
$emoji = [char]$H + [char]$L
$emoji


参考: http://www.russellcottrell.com/greek/utilities/SurrogatePairCalculator.htm

或者获取代码:

($emoji[0] - 0xD800) * 0x400 + $emoji[1] - 0xDC00 + 0x10000 | % tostring x

1f600

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接