-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path107.ps1
More file actions
70 lines (60 loc) · 1.79 KB
/
Copy path107.ps1
File metadata and controls
70 lines (60 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
## Format-HexString.ps1
## Format a byte array as a hexadecimal string
##
## Example usage:
## get-content 'c:\windows\Coffee Bean.bmp' -encoding byte | Format-HexString | more
## Convert the input to an array of bytes. This is a strongly-typed variable,
## so that we're not trying to iterate over strings, directory entries, etc.
## [byte[]] $bytes = $(foreach($byte in $input) { $byte })
begin
{
## Store our header, and formatting information
$counter = 0
$header = " 0 1 2 3 4 5 6 7 8 9 A B C D E F"
$nextLine = "{0} " -f [Convert]::ToString($counter, 16).ToUpper().PadLeft(8, '0')
$asciiEnd = ""
## Output the header
"`r`n$header`r`n"
}
process
{
## Display each byte, in 2-digit hexidecimal, and add that to the left-hand
## side.
$nextLine += "{0:X2} " -f $_
## If the character is printable, add its ascii representation to
## the right-hand side. Otherwise, add a dot to the right hand side.
if(($_ -ge 0x20) -and ($_ -le 0xFE))
{
$asciiEnd += [char] $_
}
else
{
$asciiEnd += "."
}
$counter++;
## If we've hit the end of a line, combine the right half with the left half,
## and start a new line.
if(($counter % 16) -eq 0)
{
"$nextLine $asciiEnd"
$nextLine = "{0} " -f [Convert]::ToString($counter, 16).ToUpper().PadLeft(8, '0')
$asciiEnd = "";
}
}
end
{
## At the end of the file, we might not have had the chance to output the end
## of the line yet. Only do this if we didn't exit on the 16-byte boundary,
## though.
if(($counter % 16) -ne 0)
{
while(($counter % 16) -ne 0)
{
$nextLine += " "
$asciiEnd += " "
$counter++;
}
"$nextLine $asciiEnd"
}
""
}