August 27, 2015

PowerShell - Number Conversions

To convert a decimal number to its binary representation, supply a base of 2 to the [Convert]:: ToString() method:

[Convert]::ToString(16, 2)
10000

Similarly for the reverse purpose, to convert a binary number back into its decimal representation, supply a base of 2 to the [Convert]::ToInt32() method:

[Convert]::ToInt32("10000", 2)
16

To convert a decimal number to its octal representation, supply a base of 8 to the [Convert]::ToString() method:

[Convert]::ToString(16, 8)
20

And for the reverse purpose, to convert an octal number back into its decimal representation, supply a base of 8 to the [Convert]::ToInt32() method:

[Convert]::ToInt32("20", 8)
16

To convert a decimal number to its hexa-decimal representation, supply a base of 16 to the [Convert]::ToString() method:

[Convert]::ToString(16, 16)
10

And for the reverse purpose, to convert a hexa-decimal number back into its decimal representation, supply a base of 16 to the [Convert]::ToInt32() method:

[Convert]::ToInt32("10", 16)
16

To directly enter a hexadecimal number, use the 0x prefix:

$hexNumber = 0x1111
$hexNumber

4369

No comments:

Post a Comment