Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display 3-byte unicode character in Windows PowerShell

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?

like image 262
zomega Avatar asked Dec 05 '25 18:12

zomega


1 Answers

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

๐Ÿ˜€
like image 102
js2010 Avatar answered Dec 08 '25 16:12

js2010



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!