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…
$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)…
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…
[byte]$UnicodeArray = ($UnicodeString -split " ") -replace "`r","" -replace "`n",""
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…
…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.