Tuesday, February 25, 2014

Printing Binary Numbers in Powershell

When working with numeric values and bit-flags, it is helpful to be able to see the binary representation of a value, especially when trying to understand what's going on.

Doing a search for printing a binary number in C# will get you results for functions and blocks of code that iterate through the bits. But that is completely unnecessary for this simple task.

The solution lies in the System.Converter class, which has 'toString' methods that take the base as the second argument.

Example:

PS> [convert]::ToString(0xfe, 2)
11111110