Displaying IPv4 address as Binary

by May 23, 2013

Sometimes it may be useful to display an IPv4 address bit by bit, for example, to compare it with a subnet mask. Here's another example of just how flexible PowerShell is because it only takes one line:

$ipV4 = '192.168.12.33'

-join ($ipV4.Split('.') | ForEach-Object {[System.Convert]::ToString($_,2).PadLeft(8,'0')})

The result looks like this:

You can also change the code a little:

$ipV4.Split('.') | ForEach-Object {
    '{0,5} : {1}' -f $_, [System.Convert]::ToString($_,2).PadLeft(8,'0')
 } 

Now the result looks like this:

Twitter This Tip! ReTweet this Tip!