Skip to content

Instantly share code, notes, and snippets.

@gravejester
Created February 2, 2016 14:27
Show Gist options
  • Save gravejester/f15989a933504da266c3 to your computer and use it in GitHub Desktop.
Save gravejester/f15989a933504da266c3 to your computer and use it in GitHub Desktop.
function ConvertTo-BinaryString {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true, Mandatory = $true, Position = 0)]
[array] $InputObject,
[Parameter()]
[switch] $Pad
)
PROCESS {
foreach ($item in $InputObject) {
if ($item.GetType().Name -eq 'string') {
[System.Text.Encoding]::ASCII.GetBytes($item) | ForEach-Object {
if ($Pad) {
Write-Output (([System.Convert]::ToString($_,2)).PadLeft(8,'0'))
}
else {
Write-Output ([System.Convert]::ToString($_,2))
}
}
}
else {
if ($Pad) {
Write-Output (([System.Convert]::ToString($item,2)).PadLeft(8,'0'))
}
else {
Write-Output ([System.Convert]::ToString($item,2))
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment