¿Cómo puedo codificar códigos de caracteres Unicode en un literal de cadena de PowerShell?


¿Cómo puedo codificar el carácter Unicode U+0048 (H), por ejemplo, en una cadena de PowerShell?

En C# simplemente haría esto: "\u0048", pero eso no parece funcionar en PowerShell.

Author: Peter Mortensen, 2009-06-29

3 answers

Reemplace '\u' por '0x' y envíelo al Sistema.Char:

PS > [char]0x0048
H

También puede usar la sintaxis " $ ()"para incrustar un carácter Unicode en una cadena:

PS > "Acme$([char]0x2122) Company"
AcmeT Company

Donde T es la representación de PowerShell del carácter para marcas comerciales no registradas.

 47
Author: Shay Levy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-03 20:36:03

De acuerdo con la documentación, PowerShell 6.0 agrega soporte con esta secuencia de escape:

PS> "`u{0048}"
H

Véase https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-6#unicode-character-ux

 7
Author: mclayton,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-20 10:34:17

Tal vez esta no sea la manera de PowerShell, pero esto es lo que hago. Lo encuentro más limpio.

[regex]::Unescape("\u0048") # Prints H
[regex]::Unescape("\u0048ello") # Prints Hello
 5
Author: Kevin Buchan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-03 20:39:29