While moving some data around, I found myself in need of a powershell filter to translate a hex string into its byte array equivalent. I've written this routine many times, but never quite this succinctly:
process
{
$_ -replace '^0x', '' -split "(?<=\G\w{2})(?=\w{2})" | %{ [Convert]::ToByte( $_, 16 ) }
}
My favorite part is the regex used to split the hex string - it matches nothing concrete, only lookarounds.
Use it like any other pipeline filter when you have a hex string and want a byte array; e.g.:
PS >"0x1234" | convert-fromhex
18
52
Enjoy!