note 81850 added to function.ip2long

From: Date: Sun, 16 Mar 2008 15:15:34 +0000
Subject: note 81850 added to function.ip2long
Groups: php.notes 
Request: Send a blank email to php-notes+get-137103@lists.php.net to get a copy of this message
@ samb057 at gmail dot com This function will not return a correct bigint. base_convert() is limited to the double type, which usually counts 64bits and not 128 like an IPv6 address. For example: FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF will __NOT__ be converted to 128^2 -1 You should use a bigint lib's multiplication function, with decimal shifting factors defined as string constants (much faster than recalculating them every time). Grab 8 short integers (the 16bits between the ':' with base_convert($hex,16,10)) and shift the 7 higher ones with a bigint_multiplication. Of course you can also do it with less bigint ops using 32bit integers, but then you should create them this way (additionally 'sprintf' for making an uint32 string): $uint32= sprintf('%u',intval(base_convert($hex,16,10))); Finally, "bigint_add" the shifted ones to the least (unshifted) significant interger string and be happy PS: I'm working on an IPv4/6 tool class with raw (also endianness), 6/4 compatibility and validation methods, I'll post it here as soon as it's finished and tested. ---- Server IP: 194.153.94.10 Probable Submitter: 62.202.30.173 ---- Manual Page -- http://www.php.net/manual/en/function.ip2long.php Edit -- https://master.php.net/note/edit/81850 Del: integrated -- https://master.php.net/note/delete/81850/integrated Del: useless -- https://master.php.net/note/delete/81850/useless Del: bad code -- https://master.php.net/note/delete/81850/bad+code Del: spam -- https://master.php.net/note/delete/81850/spam Del: non-english -- https://master.php.net/note/delete/81850/non-english Del: in docs -- https://master.php.net/note/delete/81850/in+docs Del: other reasons-- https://master.php.net/note/delete/81850 Reject -- https://master.php.net/note/reject/81850 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#137103) next »