php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43690 sprintf returning unexpected number
Submitted: 2007-12-27 16:24 UTC Modified: 2007-12-29 12:30 UTC
From: yardgnomeray at gmail dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.2.5 OS: Windows XP SP2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: yardgnomeray at gmail dot com
New email:
PHP Version: OS:

 

 [2007-12-27 16:24 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-28 14:45 UTC] 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-28 14:46 UTC] felipe@php.net
Ops, ignore the part "ip2long() returns a string."
 [2007-12-29 10:22 UTC] 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-29 12:15 UTC] 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 12:30 UTC] derick@php.net
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).

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 07:01:33 2025 UTC