ARZ2019
ARZ2019

Reputation: 11

Decode Hex string to UTF-16?

I am trying to understand and extract data from a database. The critical information seems to be encoded as a HEX string in one of the tables. When I tried to convert that to ASCII, it gives me gibberish. There are other files used in conjunction with this database that I believe are encoded as Unicode UTF-16. So I'd like to try and convert this hex string to UTF-16 and see what it looks like.

Can someone help me figure out how to do this in Powershell?

Thanks

Upvotes: 0

Views: 3490

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

Assuming your string consists of an even number of hexadecimal characters you could insert a delimiter character after every 2 characters, remove a trailing delimiter (to not generate a trailing empty element), and then split the string at the delimiter.

$s = 'a3422b4a'
$s -replace '..', '$&:' -replace ':$' -split ':'

Then cast the resulting fragments to bytes:

... | ForEach-Object { [byte]"0x$_" }

Collect the byte array in a variable and convert it to a Unicode string:

[Text.Encoding]::Unicode.GetString($bytes)

[Text.Encoding]::Unicode is UTF-16 LE. Use [Text.Encoding]::BigEndianUnicode if you need UTF-16 BE.


Edit: On second thought, you don't even need the loop for the conversion. You could insert the 0x before each pair of hexadecimal characters and split the string at characters followed by the sequence 0x, then cast the resulting array to a byte array.

[byte[]]($s -replace '..', '0x$&' -split '(?<=.)(?=0x)')

Upvotes: 1

Related Questions