-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathConvertTo-Binary.ps1
74 lines (65 loc) · 2.03 KB
/
ConvertTo-Binary.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function ConvertTo-Binary {
<#
.SYNOPSIS
Convert an integer or array of integers to binary
.DESCRIPTION
Convert an integer or array of integers to binary
.PARAMETER Number
An array of integers that you want converted to binary
.PARAMETER MinimumWidth
Minimum number of characters that the binary representation will be. The binary number could be longer than the minimum width. Aliased to 'Width'.
.PARAMETER IncludeInput
Switch indicating that you want the input to be included in the output
.EXAMPLE
ConvertTo-Binary -Number 23
10111
.EXAMPLE
ConvertTo-Binary -Number 32,0xff -IncludeInput
Number Binary
------ ------
32 100000
255 11111111
.EXAMPLE
ConvertTo-binary -Number 32,0xff -IncludeInput -MinimumWidth 16
Number Binary
------ ------
32 0000000000100000
255 0000000011111111
.NOTES
Changed to use unsigned 64 bit values so that larger numbers can be processed
#>
#region Parameter
[CmdletBinding(ConfirmImpact = 'Low')]
[OutputType('string')]
Param(
[Parameter(Mandatory,HelpMessage='Enter an integer value(s)', Position = 0, ValueFromPipeline)]
[uint64[]] $Number,
[ValidateRange(1,255)]
[Alias('Width')]
[int] $MinimumWidth,
[switch] $IncludeInput
)
#endregion Parameter
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
}
process {
foreach ($curNumber in $Number) {
$ReturnVal = [convert]::ToString($curNumber, 2)
if ($MinimumWidth) {
$ReturnVal = $ReturnVal.PadLeft($MinimumWidth, '0')
}
if ($IncludeInput) {
New-Object -TypeName psobject -Property ([ordered] @{
Number = $curNumber
Binary = $ReturnVal
})
} else {
Write-Output -InputObject $ReturnVal
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}