Functions/ConvertFrom-Hex.ps1

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
function ConvertFrom-Hex {
<#
.SYNOPSIS
    Convert an string or string array from hexadecimal to an integer
.DESCRIPTION
    Convert an string or string array from hexadecimal to an integer
.EXAMPLE
    ConvertFrom-Hex -Hex 'f0','20'
 
    240
    32
.EXAMPLE
    ConvertFrom-Hex -Hex '0xff','20' -IncludeInput
 
    Hex Number
    --- ------
    0xff 255
    20 32
.NOTES
    Changed to use unsigned 64 bit values so that larger numbers can be processed
#>


    #region Parameter
    [CmdletBinding(ConfirmImpact = 'Low')]
    [OutputType('int')]
    Param(
        [Parameter(Mandatory,HelpMessage='Enter a hexadecimal string', Position = 0, ValueFromPipeline)]
        [string[]] $Hex,

        [switch] $IncludeInput
    )
    #endregion Parameter

    begin {
        Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
    }

    process {
        foreach ($curHex in $Hex) {
            $ReturnVal = [convert]::ToUInt64($curHex, 16)
            if ($IncludeInput) {
                New-Object -TypeName psobject -Property ([ordered] @{
                        Hex  = $curHex
                        Number = $ReturnVal
                    })
            } else {
                Write-Output -InputObject $ReturnVal
            }
        }
    }

    end {
        Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
    }
}