Google

Saturday, February 2, 2008

IP Octets (011010010111000000100000011011110110001101110100011001010111010001110011)

First off the binary in the ()'s is ascii binary conversion of ip octets :P Anyhoo, a buddy of mine brought it to my attention last night that most applications can use just three octets as opposed to all four. So it would be w.x.y.z for four correct? I hope you said yes -.- Anyway, so to make it three it would be w.x.y*(256)+z. So we thought hey, how far can we go with this. So we tried making it just two! w.x*(256 squared)+y*(256)+z and it worked! Next I said hey, I'm a genius lets try figuring the math for just one! So, w*(256 cubed)+x*(256 squared)+y*(256)+z. And of course it worked! So I decided to write a shell script for to do this. And here is said script for those of you using *nix instead of windoze:
x="$1"
x1=`echo $x | awk -F . {'print $1'}`
x2=`echo $x | awk -F . {'print $2'}`
x3=`echo $x | awk -F . {'print $3'}`
x4=`echo $x | awk -F . {'print $4'}`
grr=`expr "256" "*" "256" "*" "256"`
y0=`expr "$x1" "*" "$grr"`
y1=`expr "$x2" "*" "65536"`
y2=`expr "$x3" "*" "256"`
outcome3=`expr "$y2" + "$x4"`
outcome2=`expr "$y1" "+" "$y2" + "$x4"`
outcome1=`expr "$y0" + "$y1" "+" "$y2" + "$x4"`
echo "------------------------------------------"
echo "Input IPv4: $x"
echo "------------------------------------------"
echo "Outcome with 3 Octets: $x1.$x2.$outcome3"
echo "Outcome with 2 Octets: $x1.$outcome2"
echo "Outcome with 1 Octets: $outcome1"

No comments: