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.

 $string = "The quick brown dog"; 
[string]::Join($null, ([char[]]$string | % { "{0:X}" -f [int]$_; }));

This returns:

 54686520717569636B2062726F776E20646F67

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:

 $hex = "54686520717569636B2062726F776E20646F67";
[char[]]$hex | % -begin { 
        $i = 0; 
        [string]$s = $null; 
    } -process { 
        if ($i % 2) 
        { 
            $s + $_; 
            [string]$s = $null; 
        } 
        else 
        { 
            $s += $_; 
        } 
        $i +=1; 
    }

Perfectly valid.  However, PowerShell provides an obscure, yet incredibly succinct way to do this:

 $hex = "54686520717569636B2062726F776E20646F67"; $hex -split '(..)' | ? { $_ }

Three notes:

- 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.

$hex = "54686520717569636B2062726F776E20646F67"; [char[]]$hex | % -begin { $i = 0; [string]$s = $null; } -process { if ($i % 2) { $s + $_; [string]$s = $null; } else { $s += $_; } $i +=1; }

Why would you want to do that? You guessed it: no idea.