Configuration PowerShell Troubleshooting

PowerShell – Convert Special Character String Into Numbers

Unicode

Do you know the situation where you have to handle strings / string data that has a lot of complex characters? In certain situation it is just a pain to deal with strings that have special characters like “*ç%&/()=`è or even an entire Export-CliXML output content. Recently I had the need to embed such data into an existing configuration file. Meaning I had a configuration file and within that file an attribute / property had many different special characters from the based on Unicode characters. To avoid any issues, like copy/paste problems, mistyping, misinterpretation, conversion issues etc. with those special characters. I wanted a way to convert this string into numbers. Having numbers in place will avoid any of the above mentioned problems, because there is an unambiguous assignment.

Here I would like to provide you two functions to convert a complex string with special characters into Unicode byte array and a function to convert the byte array back into the string.

Convert to Unicode…

Function ConvertTo-Unicode
{
param
(
[Parameter(Mandatory=$true)]
$String
)
$Bytes = [System.Text.Encoding]::Unicode.GetBytes($String)
return $Bytes -join " "
}

…this will convert a string like !èà£:éà_”+*ç%&/()==?  into an array of numbers (Unicode UTF-16 format using the little endian byte order)…

image

Now you simply copy these numbers into your text file or whatever place your string is needed.

To convert it back you simply use this function…

Function ConvertFrom-Unicode
{
param
(
[Parameter(Mandatory=$true)]
$UnicodeString
)
[byte[]]$UnicodeArray = ($UnicodeString -split " ") -replace "`r","" -replace "`n",""
return [System.Text.Encoding]::Unicode.GetString($UnicodeArray)
}

image

The function will replace any carriage return (`r) and also new line (`n) from the array to avoid any errors.

One of the most popular way to convert complex data is probably to use base64 encoding. This is certainly a good practice, but if you look close at a base64 encoded string like here…

image

…you will see that there are special characters again.

I think these two functions are a neat little way for handling such situations.

Let me know your approach, have fun.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.