Converting Numbers

by Jan 8, 2009

The .NET convert class is a great help when you need to convert numbers between different systems. Here's how you can convert a decimal into a binary in one line:

$number = 12756576
[Convert]::ToString($number, 2)

The toString() method takes your number and the base you would like to use. For binary representation, choose base 2. To convert to hexadecimal, use 16:

[Convert]::ToString($number, 16)

The opposite works, too. Use this approach to convert a binary to a decimal:

$binary = '1110111000010001'
[Convert]::ToInt64($binary, 2)