Decimal and hexadecimal conversion issues in PHP

Source: Internet
Author: User
$s = '9C216AFB868C';$sb=hexdec($s) ;var_dump( $sb );//1.716673427227E+14echo '
';var_dump( dechex($sb) );//6afb868c

The hexadecimal $s is converted to decimal, and then the hex is reversed.

dechex()there is a limit to the size of the conversion.
------------Supplement---------------------------------------
$s = ' 9c216afb868c ';
How does the $s plus 1 php come true? (hexadecimal calculation)

Reply content:

$s = '9C216AFB868C';$sb=hexdec($s) ;var_dump( $sb );//1.716673427227E+14echo '
';var_dump( dechex($sb) );//6afb868c

The hexadecimal $s is converted to decimal, and then the hex is reversed.

dechex()there is a limit to the size of the conversion.
------------Supplement---------------------------------------
$s = ' 9c216afb868c ';
How does the $s plus 1 php come true? (hexadecimal calculation)

In contrast, the final output 6afb868c is exactly 9C216AFB868C the back 8 bits (ignoring case).

In the HEXDEC manual, the following:

Starting with PHP 4.1.0, this function can handle an integer large number, in which case it returns a float type.

In the Dechex:

The maximum value that can be converted is the decimal Php_int_max * 2 + 1 (or-1): The 32-bit platform is a decimal 4294967295, and the result of its dechex () is ffffffff.

The first result 1.716673427227E+14 is written in normal format 171667342722700 , and the result has no problem.

9c216afb868cGo to decimal: http://www.wolframalpha.com/input/?i=convert++9c216afb868c+to+base+10

This number is again converted back to be 9c216afb868c .

171667342722700Ext. 16: Http://www.wolframalpha.com/input/?i=171667342722700+to+hex

First of all, if you are not a 32-bit computer, this number is far from the maximum value that PHP can handle, the maximum value in PHP can be PHP_INT_MAX found in the constant, under the 64-bit computer its value is

echo PHP_INT_MAX;    // 9223372036854775807echo dechex(PHP_INT_MAX); // 7fffffffffffffff

And you can be successful from 16 into the system, it is impossible to turn back because of the size problem . The problem is that the 10 binary value that you turn out is

1.716673427227E+14

This is a string expressed in scientific notation, and the dechex function is not able to recognize the string, andPHP will only automatically convert a string of pure numbers into a corresponding shaping or floating point .

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.