Splitting a Hex-Encoded String into Pairs of Hex Characters (a.k.a. To Pull a Noah)

Simple enough task: I have a hex-encoded string and need to decode it. Now, we all know that to encode a string to hex is to cast each [char] to [int], then shove it through the "{0:X}" format specifier, then concatenate all the strings.

And we know to decode a hex-encoded character, we shove each pair of hex characters through the reverse process (more or less):

[char][Convert]::ToInt16('54', 16);

T

However, how do we split a string into pairs? After all, the above magic only works for pairs of hex characters. We can cast it into a [char[]] array, then iterate over the array to concatenate every two characters back to a bunch of two-character strings:

- The "| ? { $_; }" filter (effectively equivalent to IsNotNull()) is required. Otherwise, the split has the nasty habit of interleaving $null elements in the returned list of strings. I have no idea why.

- The parentheses in the "'(..)'" split specifier is also required. "'..'" matches on the literal twin periods. Why the split specifier becomes a RegEx when parenthesized? No idea.

- For those who like Perl’s readability, you can smash the loop into a single line so it’s as inobvious as the "-split '(..)'" split specifier.