I want to support Unicode and as most characters as possible in my PowerShell script. As encoding I want to use UTF-8. So for testing purposes I simply type this line and press enter:
[char]0x02A7
And it successfully shows the character สง.
But when I try to display a Unicode character (> 0xFFFF):
[char]0x01F600
It throws an error telling that the value 128512 cannot be converted to System.Char. Instead it should show the smiley ๐.
What is wrong here?
Edit:
As Jeroen Mostert stated in the comments, I have to use another command for unicode characters with code point > 0xFFFF. So I wrote this script:
$s = [Char]::ConvertFromUtf32(0x01F600)
Write-Host $s
In the PowerShell IDE I get a beautiful smiley ๐. But when I run the script standalone (in an own window) I don't get the smiley. Instead it shows two strange characters.
What is wrong here?
Aside from [Char]::ConvertFromUtf32(), here's a way to calculate the surrogate pair by hand for code points over 2 bytes or 16 bits long (http://www.russellcottrell.com/greek/utilities/surrogatepaircalculator.htm):
$S = 0x1F600
[int]$H = [Math]::Truncate(($S - 0x10000) / 0x400) + 0xD800
[int]$L = ($S - 0x10000) % 0x400 + 0xDC00
[char]$H + [char]$L
๐
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With