Share via


PowerShell Tip: Converting String to Character Array

Summary

Converting String to Character array.

Requirement

Convert string to ASCII

Error

**Cannot convert value "" to type "System.Char". Error: "String must be exactly one character long."


**

Code Used

'String' -split '' | %{[int][char]$_}

Solution

Indeed PowerShell did the correct job. Before doing it we need to convert the string to a character array. It's throwing an error because of the white space character.

Fixed Code

'String'.ToCharArray() | %{[int][char]$_}
[char[]]'String' | %{[int][char]$_}

Output

Video Reference

View
Enjoy PowerShell :)