header image

WMF 5.0 April 2015 preview – – Format-Hex

Posted by: | May 1, 2015 Comments Off on WMF 5.0 April 2015 preview – – Format-Hex |

Have you ever needed to generate a hex representation of a string or binary data?

Say you have a string – ‘PowerShell Rocks’

 

And you want to go the hex representation which is

50 6f 77 65 72 53 68 65 6c 6c 20 52 6f 63 6b 73

 

You would have to do something like this

$hexary = @()
$chars = ‘PowerShell Rocks’ -split ”

foreach ($char in $chars) {
if ($char -ne ”) {
   $hexary += [convert]::ToString(([byte][char]$char),16)
}
}

$hexary -join ‘ ‘

 

Split the string into an array of strings each of a single character. If the string is not empty convert it to a [char] then a [byte] and finally a string formatted as hex.

 

The April 2015 WMF 5.0 preview simplifies that process

£> ‘PowerShell Rocks’ | Format-Hex

                   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   50 6F 77 65 72 53 68 65 6C 6C 20 52 6F 63 6B 73  PowerShell Rocks

 

You can also view binary files such as Word documents in .doc format

under: PowerShell v5

Comments are closed.

Categories