PoshCode Archive  Artifact [eac44c0c96]

Artifact eac44c0c960237682cd3f74b58d39b97298d3641fdb89cbe475564f9e558e20a:

  • File Get-HexDump.ps1 — part of check-in [90f19a1017] at 2018-06-10 13:43:28 on branch trunk — Fixed (user: greg zakharov size: 1504)

# encoding: ascii
# api: powershell
# title: Get-HexDump
# description: Fixed
# version: 0.1
# type: function
# author: greg zakharov
# license: CC0
# function: Get-HexDump
# x-poshcode-id: 4593
# x-archived: 2016-05-29T07:49:47
# x-published: 2016-11-08T04:34:00
#
#
#requires -version 2.0
Set-Alias od Get-HexDump

function Get-HexDump {
  [CmdletBinding(DefaultParameterSetName="FileName", SupportsShouldProcess=$true)]
  param(
    [Parameter(Mandatory=$true,
               Position=0,
               ValueFromPipeline=$true)]
    [ValidateNotNullOrEmpty()]
    [String]$FileName,
    
    [Parameter(Mandatory=$false,
               Position=1)]
    [ValidateRange(0, 65535)]
    [Int32]$BytesToProcess = -1
  )
  
  begin {
    $ofs = ''
    [Int32]$line = 0
  }
  process {
    switch ((Test-Path $FileName)) {
      $true{
        if ($PSCmdlet.ShouldProcess($FileName, "Get hex dump of")) {
          gc -ea 0 -enc Byte $FileName -re 16 -to $BytesToProcess | % {
            '{0:X8} {1, -49} {2}' -f $line++, [String](
              $_ | % {' ' + ('{0:x}' -f $_).PadLeft(2, "0")}
            ), [String](
              $_ | % {
                if ([Char]::IsLetterOrDigit($_) -or [Char]::IsSymbol($_) `
                  -or [Char]::IsPunctuation($_)) {[Char]$_}
                else {'.'}
              }
            )
          }
        }
      }
      default{Write-Warning "file not found or does not exist."}
    }
  }
  end {}
}