March 16, 2015

It's all about the BASE (2,8,10,16)... Part 1

Grumpy Admin was asked a quick question this afternoon – How do I display a number in binary in Powershell. I can only assume his context was IP address or Subnet mask, I think he was trying something with wildcard masks on a firewall…. Not sure, Grump Admin doesn’t really pay much attention unless it means he has to do actual work!  Well to answer his question – I discovered a while ago that it is easy to convert a number to base 2 and back again in Powershell. In fact converting to either base 2 10 8 and 16 are all possible with a single Powershell command! So I told him use System.Convert. He then asked if I could do a quick “demo” for him, I can only assume he was too freaking lazy to use the Google machine! This made me grumpy but as he asked nice and just made the brews, I thought I would knock something up for him!

So using the [System.Convert] power!  I converted 255 in to 11111111 for him right before his eyes!!!

[System.Convert]::tostring(255,2)

base1

And to convert a number to base 16 will be like this

[System.Convert]::tostring(255,16)

base2

He was happy with that, then I thought, it is a good tip but not much substance to it for my little blog,so to make it look like I am doing work and not blogging I thought I would make it a little bit more complex! Let’s do this with the whole IP addresses!

So the [System.convert] works for a single portion of an IP address, but how can we do this to a full IP address? Well as you know IP addresses tend to have “.” inside of them – so we need to strip this out to start with.

$IPaddress=”10.0.0.1″
$ipraw = $ip -split “\.”

So if we run this we will get $ipraw = 10 0 0 1 as you can see we split it just before the .

This is great so all we need to do is loop through the $ipraw and convert

$IPaddress=”10.0.0.1″
$ipraw = $ipaddress -split “\.”| ForEach-Object {[System.Convert]::ToString($_,2)}
$ipraw

Which gives us :-

1010
0
0
1

Which is correct 🙂 but it’s not very ummm Admin friendly really is it!  Grumpy Admin doesn’t like it when it isn’t pretty! We like things in rows of 8 when dealing with binary in the computer world don’t we people! So it is time to bring in one of my all-time top ten commands, you guessed it – the Get-Member command!

[System.Convert]::ToString($_,2)|Get-Member

Base3

As you can see there is a padleft and padright option… we want to padleft to size of 8 with a char of “0”, so our new convert command now looks like this!

$IPaddress=”10.0.0.1″
$ipraw = $ipaddress -split “\.”| ForEach-Object {[System.Convert]::ToString($_,2).PadLeft(8,”0”)}
$ipraw

This doesn’t look to neat so let’s do a -join at it to get back in to a single line for our eyes….

$ipraw -join ” “

Now to get all cisco on us, lets duplicate this and put the subnet in as well – Grumpy Admin is lazy so just replicates the code with new variable names and write-host the result. Job Done!

$IPaddress=”10.0.0.1″
$subnet=”255.255.255.0″
$ipraw = $ipaddress -split “\.”| ForEach-Object {[System.Convert]::ToString($_,2).PadLeft(8,”0”)}
$subnetraw = $subnet -split “\.”| ForEach-Object {[System.Convert]::ToString($_,2).PadLeft(8,”0″)}
write-host $($ipraw -join ” “)`n$($subnetraw -join ” “)

There wasn’t that easy, now you have a snippet of code that will convert an IP and SubNet into binary…. Next blog (part 2), We shall spin things around and convert from binary back to IP which is just the reverse… oh exciting…

Hazzy