-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConvertTo-HexString.ps1
More file actions
97 lines (91 loc) · 4.16 KB
/
ConvertTo-HexString.ps1
File metadata and controls
97 lines (91 loc) · 4.16 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<#
.SYNOPSIS
Convert to Hex String
.DESCRIPTION
.EXAMPLE
PS C:\>ConvertTo-HexString "What is a hex string?"
Convert string to hex byte string seperated by spaces.
.EXAMPLE
PS C:\>"ASCII string to hex string" | ConvertTo-HexString -Delimiter "" -Encoding Ascii
Convert ASCII string to hex byte string with no seperation.
.INPUTS
System.Object
#>
function ConvertTo-HexString {
[CmdletBinding()]
param (
# Value to convert
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[object] $InputObjects,
# Delimiter between Hex pairs
[Parameter (Mandatory = $false)]
[string] $Delimiter = ' ',
# Encoding to use for text strings
[Parameter (Mandatory = $false)]
[ValidateSet('Ascii', 'UTF32', 'UTF7', 'UTF8', 'BigEndianUnicode', 'Unicode')]
[string] $Encoding = 'Default'
)
begin {
function Transform ([byte[]]$InputBytes) {
[string[]] $outHexString = New-Object string[] $InputBytes.Count
for ($iByte = 0; $iByte -lt $InputBytes.Count; $iByte++) {
$outHexString[$iByte] = $InputBytes[$iByte].ToString('X2')
}
return $outHexString -join $Delimiter
}
## Create list to capture byte stream from piped input.
[System.Collections.Generic.List[byte]] $listBytes = New-Object System.Collections.Generic.List[byte]
}
process {
if ($InputObjects -is [byte[]]) {
Write-Output (Transform $InputObjects)
}
else {
foreach ($InputObject in $InputObjects) {
[byte[]] $InputBytes = $null
if ($InputObject -is [byte]) {
## Populate list with byte stream from piped input.
if ($listBytes.Count -eq 0) {
Write-Verbose 'Creating byte array from byte stream.'
Write-Warning ('For better performance when piping a single byte array, use "Write-Output $byteArray -NoEnumerate | {0}".' -f $MyInvocation.MyCommand)
}
$listBytes.Add($InputObject)
}
elseif ($InputObject -is [byte[]]) {
$InputBytes = $InputObject
}
elseif ($InputObject -is [string]) {
$InputBytes = [Text.Encoding]::$Encoding.GetBytes($InputObject)
}
elseif ($InputObject -is [bool] -or $InputObject -is [char] -or $InputObject -is [single] -or $InputObject -is [double] -or $InputObject -is [int16] -or $InputObject -is [int32] -or $InputObject -is [int64] -or $InputObject -is [uint16] -or $InputObject -is [uint32] -or $InputObject -is [uint64]) {
$InputBytes = [System.BitConverter]::GetBytes($InputObject)
}
elseif ($InputObject -is [guid]) {
$InputBytes = $InputObject.ToByteArray()
}
elseif ($InputObject -is [System.IO.FileSystemInfo]) {
if ($PSVersionTable.PSVersion -ge [version]'6.0') {
$InputBytes = Get-Content $InputObject.FullName -Raw -AsByteStream
}
else {
$InputBytes = Get-Content $InputObject.FullName -Raw -Encoding Byte
}
}
else {
## Non-Terminating Error
$Exception = New-Object ArgumentException -ArgumentList ('Cannot convert input of type {0} to Hex string.' -f $InputObject.GetType())
Write-Error -Exception $Exception -Category ([System.Management.Automation.ErrorCategory]::ParserError) -CategoryActivity $MyInvocation.MyCommand -ErrorId 'ConvertHexFailureTypeNotSupported' -TargetObject $InputObject
}
if ($null -ne $InputBytes -and $InputBytes.Count -gt 0) {
Write-Output (Transform $InputBytes)
}
}
}
}
end {
## Output captured byte stream from piped input.
if ($listBytes.Count -gt 0) {
Write-Output (Transform $listBytes.ToArray())
}
}
}