Bur0k
Bur0k

Reputation: 136

Writing a hex escaped char in Powershell

Is there any way to write something like this in Powershell? (Linux would be with Perl)

char foo = '\x41';

I need to input some non-printable characters to one of my programs

Upvotes: 7

Views: 8240

Answers (3)

Albert
Albert

Reputation: 396

I’d like to offer a different approach than casting as hex. Instead of using hex, PowerShell 7 supports Unicode character encoding (see article about_special_characters > Unicode characters). So, with x041 as Latin capital A, the Unicode equivalent would be "`u{41}". The Microsoft docs example demonstrated with this example: "`u{2195}" (note the backtick precedes the letter).

Upvotes: 5

WalterH
WalterH

Reputation: 109

Short form is:

$foo = [char]0x41

Upvotes: 0

Perfect28
Perfect28

Reputation: 11327

You can do it by casting an int to char.

With a decimal number :

 $foo = (65 -as [char])

And from hexa :

 $foo = (0x41 -as [char])

Both will set $foo to A

Upvotes: 6

Related Questions