Casting Strings

by Jun 9, 2009

Strings represent text information and consist of individual characters. By casting, you can convert strings to individual characters and these into numeric ASCII codes:

[char[]]“Hello”
[int[]][char[]]“Hello”

Casting helps you to convert text to ascii code and vice versa, too. The following statement raises an exception because PowerShell cannot directly convert a string into an ascii value:

[int]“H”

However, you can combine casts and first cast to a single character and then to an integer. The result is the ascii code:

[int][char]“H”

You can use this to go the other way and convert a numeric value to a character:

[char]72