Re: IP address enconding

From: Date: Thu, 19 Apr 2001 19:00:27 +0000
Subject: Re: IP address enconding
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-49398@lists.php.net to get a copy of this message
On 19-Apr-01 elias wrote: > No! I can encode IP address to an Integer: > 192.168.0.1 consider as: a.b.c.d > and now the formula goes: > a shl 24 + b shl 16 + c shl 8 + d > > but i was looking for something ready made.... > PHP3 (returns a float, not an integer): function inet_aton($a) { $inet = 0.0; $t = explode(".", $a); for ($i = 0; $i < 4; $i++) { $inet *= 256.0; $inet += $t[$i]; }; return $inet; } function inet_ntoa($n) { $t=array(0,0,0,0); $msk = 16777216.0; $n += 0.0; if ($n < 1) return('&nbsp;'); for ($i = 0; $i < 4; $i++) { $k = (int) ($n / $msk); $n -= $msk * $k; $t[$i]= $k; $msk /=256.0; }; $a=join('.', $t); return($a); } Regards, -- Don Read dread@texas.net -- It's always darkest before the dawn. So if you are going to steal the neighbor's newspaper, that's the time to do it.

« previous php.general (#49398) next »