Tuesday, August 7, 2007

IP decimal to binary conversion and back

 

Just playing around with IP numbers. here are two PowerShell one-liners for the job.

 

$ip = "192.168.0.1";

# convert to binary form
$sbin=[string]::join(".",($ip.split(".") | % {[System.Convert]::ToString($_,2).PadLeft(8,"0")}))   
$sbin


11000000.10101000.00000000.00000001

# convert the result back to decimal
[string]::join(".",($sbin.split(".") | % {[System.Convert]::ToByte($_,2)}))
192.168.0.1

This one-liners can be converted to functions in order to use them in the pipeline thus converting bulk ip numbers easily and PowerShelly ;)

1 comment:

Anonymous said...

Thanks a lot, I was looking for exactly this :)