IP Conversion code
* While automating some reporting functions recently, I came across an IP address notated as a ten digit number instead of the more standard ‘dotted-quad’ notation (aaa.bbb.ccc.ddd) While technically correct, the ten digit representation wasn’t as descriptive as I would have liked it, so it needed to be converted to ‘dotted-quad’. In dotted-quad notation each octet or section represents a power of 256. The first octet represents 256 raised to the third power, the second octet is 256 raised to the second power and so on. This is important when decoding the ten-digit number. To convert the ten-digit number to dotted-quad start by dividing the ten-digit number by 256^3 (256*256*256 or 16777216) The first three digits represent the first octet. Subtract the product of 256^3 and the first octet from the original ten digit number and then divide that by 256^2. The first three digits are the second octet. Repeat the procedure for the remaining octets. Confused? Here’s some sample code:
$addr=3236053395;
$o1=substr($addr/16777216,0,3);
$addr=$addr-($o1*16777216);
$o2=substr($addr/65536,0,3);
$addr=$addr-($o2*65536);
$o3=substr($addr/256,0,3);
$o4=$addr-($o3*256);
print("$o1.".".$o2.".".$o3.".".$o4."\n");
