I often have to do conversions between decimal, hex and binary so I decided I wanted some functions to do this for me
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061
|
#Requires -version 2.0 ## ## Author Richard Siddaway ## Version 0.1 – Inital Release May 2009 ###############################################
function Convert-ToHex{ [CmdletBinding()] param ( [Parameter(ValueFromPipeline=$true)] [int64]$inputvalue =0 ) [convert]::ToString($inputvalue,16) }
function Convert-ToBinary{ [CmdletBinding()] param ( [Parameter(ValueFromPipeline=$true)] [int64]$inputvalue =0 ) [convert]::ToString($inputvalue,2) }
function Convert-ToDecimal{ [CmdletBinding()] param ( [Parameter(ValueFromPipeline=$true)] [string]$inputvalue,
[Parameter(ParameterSetName="Binary")] [switch]$binary, [Parameter(ParameterSetName="Hex")] [switch]$hex ) $chars = $inputvalue.Trim().ToCharArray() switch ($psCmdlet.ParameterSetName) { Binary { foreach ($char in $chars){ if (-not($char -match ‘0|1’)){ Throw "$inputvalue is not a valid binary number" } } $ret = [convert]::ToInt64($inputvalue.Trim(),2) break } Hex { foreach ($char in $chars){ if (-not($char -match ‘[0-9]’ -or $char -match ‘[A-F]’)){ Throw "$inputvalue is not a valid hex number" } }
$ret = [convert]::ToInt64($inputvalue.Trim(),16) break } } return $ret }
|
Three simple functions to perform the conversions.
In the convert-todecimal I check that the input string is correct for binary or hex respectively before performing the conversion. It returns a int64 but that is easy to convert to int32 if required
[int]$i32 = Convert-ToDecimal -inputvalue "FFFFFF" -hex
Use the functions like this
Convert-ToBinary 255
Convert-ToHex 255
Convert-ToDecimal -inputvalue "ff" -hex
Convert-ToDecimal -inputvalue "11111111" –binary
or even
Convert-ToHex -inputvalue (Convert-ToDecimal -inputvalue "11111111" -binary)
though this is messy so I’m going to create direct hex <-> binary conversion routines
Error trapping stops false conversions
PS> Convert-ToDecimal -inputvalue " 111b1111 " -binary
111b1111 is not a valid binary number
At C:\Scripts\Modules\pammath\pammath.psm1:42 char:34
+ Throw <<<< "$inputvalue is not a valid binary number"
+ CategoryInfo : OperationStopped: ( 111b1111 is not a valid binary number:String) [], RuntimeException
+ FullyQualifiedErrorId : 111b1111 is not a valid binary number
PS> Convert-ToDecimal -inputvalue "ffg" -hex
ffg is not a valid hex number
At C:\Scripts\Modules\pammath\pammath.psm1:52 char:34
+ Throw <<<< "$inputvalue is not a valid hex number"
+ CategoryInfo : OperationStopped: (ffg is not a valid hex number:String) [], RuntimeException
+ FullyQualifiedErrorId : ffg is not a valid hex number
When I’ve finished the direct hex <-> binary conversions this will be the second module on PSAM. I’ll add some functions to do simple arithmetic in hex and binary as well