Tuesday, July 1, 2008

PowerShell: Get File Contents in Hex

I've done this a couple times and thought I'd share.

Occasionally I need to get the hexadecimal representation of a file so I can specify it as the value of a VarBinary variable in SQL Server for testing.

You can do this in two lines with powershell:
> get-content -Encoding byte yourfile.txt | %{ "{0:x}" -f $_ } | %{ if ( $_.Length -eq 1 ) { $out = $out + 0 + $_ } else { $out = $out + $_ } }
> $out


The first part:
get-content -Encoding byte yourfile.txt
Outputs the file's contents as bytes.

The second part:
| %{ "{0:x}" -f $_ }
Converts the bytes to hexadecimal representation.

The last part:
| %{ if ( $_.Length -eq 1 ) { $out = $out + 0 + $_ } else { $out = $out + $_ } }
Corrects any single hex character values so that they display preceded by a 0 and concatenates them into a string so that they you can output them all on a single line.

Anyone know of any ways to shorten this script up?

3 comments:

  1. Here's a shorter command:

    gc -en byte file.txt | % { '{0:X2}' -f $_ } | Join-String

    If you use it a lot:

    function Format-HexString {
    param([string[]]$path)
    gc -en byte $path | % { '{0:X2}' -f $_ } | Join-String
    }

    New-Alias fhs Format-HexString

    fhs file.txt

    You've gotta love PowerShell! :-)

    ReplyDelete
  2. Very cool!

    I didn't know about the X2 trick to automatically pad the digits, that's great.

    But I see that Join-String doesn't come with Powershell. You have to install the Powershell Community Extensions: http://www.codeplex.com/PowerShellCX

    Thanks for the tip!

    ReplyDelete
  3. Oups, I always run with PSCX installed, so I didn't remember Join-String isn't a standard cmdlet. Anyway, if you for some reason cannot install PSCX, you can easily create a replacement:

    function Join-String {
       begin {
          $sb = New-Object System.Text.StringBuilder
       }
       process {
          $sb.Append($_) | Out-Null
       }
       end {
          $sb.ToString()
       }
    }

    Hope it's of any use!

    ReplyDelete

Note: Only a member of this blog may post a comment.