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 = "";

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


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

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 :)