#43690 [Opn->Bgs]: sprintf returning unexpected number
| From: | derick@php.net | Date: | Sat, 29 Dec 2007 12:30:24 +0000 |
| Subject: | #43690 [Opn->Bgs]: sprintf returning unexpected number | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-120928@lists.php.net to get a copy of this message | ||
ID: 43690
Updated by: derick@php.net
Reported By: yardgnomeray at gmail dot com
-Status: Open
+Status: Bogus
Bug Type: Strings related
Operating System: Windows XP SP2
PHP Version: 5.2.5
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Nothing is wrong here:
$ip = "213.10.212.144";
var_dump(ip2long( $ip ) );
returns: int(-720710512)
$ip = "213.10.212.144";
$ipaddr = sprintf("%u",ip2long($ip));
var_dump( $ipaddr );
returns: string(10) "3574256784"
Using a string that does not fit in the integer range as a number (with
%d or %u) does not work, as the string itself is first converted to an
integer - which won't work as it doesn't fit. You shouldn't simply use
%u for the returned variable from sprintf() - as it's not a n integer,
but a string (so use %s).
Previous Comments:
------------------------------------------------------------------------
[2007-12-29 12:15:50] felipe@php.net
I'm using PHP 5.3.0-dev (cli) (built: Dec 25 2007 10:07:55), and the
convertion was necessary.
------------------------------------------------------------------------
[2007-12-29 10:22:44] derick@php.net
@felipe... you're wrong here - this works just fine without having to
cast to float:
derick@kossu:~$ php-5.2.5RC2
<?php
$ip = "213.10.212.144";
$ipaddr = sprintf("%u",ip2long($ip));
$address = sprintf("IP ADDR = %u", $ipaddr);
echo $address;
?>
IP ADDR = 3574256784d
------------------------------------------------------------------------
[2007-12-28 14:46:42] felipe@php.net
Ops, ignore the part "ip2long() returns a string."
------------------------------------------------------------------------
[2007-12-28 14:45:19] felipe@php.net
ip2long() returns a string. Converting this value to float for obtain
large precision, you will have the expected result.
$ip = "213.10.212.144";
$ipaddr = sprintf("%u", ip2long($ip));
$address = sprintf("IP ADDR = %u", (float) $ipaddr);
var_dump(PHP_INT_MAX, ip2long($ip), $ipaddr, $address);
--
int(2147483647)
int(-720710512)
string(10) "3574256784"
string(20) "IP ADDR = 3574256784"
------------------------------------------------------------------------
[2007-12-27 16:24:54] yardgnomeray at gmail dot com
Description:
------------
Using integer >= 2147483647 for a %u or %d will return 2147483647
Reproduce code:
---------------
$ip = "213.10.212.144";
$ipaddr = sprintf("%u",ip2long($ip));
$address = sprintf("IP ADDR = %u", $ipaddr);
echo $address;
Expected result:
----------------
IP ADDR = 3574256784
Actual result:
--------------
IP ADDR = 2147483647
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=43690&edit=1