Splitting Hexadecimal Pairs

by Dec 27, 2012

If you'd have to process a long list of encoded information, let's say a list of hexadecimal values, how would you split the list into pairs of two? Here is a way:

'this gets splitted in pairs of two' -split '(?<=\G.{2})(?=.)'
th
is
 g
et
s
sp
(...)

This would be more specific and split only hex values:

'00AA1CFFAB1034' -split '(?<=\G[0-9a-f]{2})(?=.)'

Now it's easy to reformat MAC addresses as well:

PS> '00AA1CFFAB1034' -split '(?<=\G[0-9a-f]{2})(?=.)' -join ':'
00:AA:1C:FF:AB:10:34

Twitter This Tip! ReTweet this Tip!