2

I am using two ways to convert a hexadecimal string to decimal. Using intval function in which I am passing 16 base as the second parameter to convert the hexa decimal string in decimal. same thing I am doing using hexdec built-in function. Both returning different answer.

May I know the reason behind these two function's output?

$a = 'efdebd76';
echo 'intvar : '. intval($a,16);
echo '<br/>';
echo 'hexdec : '.hexdec($a);

Output

intvar : 2147483647

hexdec : 4024352118

1 Answer 1

2

from php manual about "intval" function:

Return Values: The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807.

so I guess you run intval on 32 bit system because it return you the value 2147483647, and the value of 0xefdebd76 is bigger than the maximum Integer value. in hex_string, it's seems that it not limited by the system you execute this function.

Sign up to request clarification or add additional context in comments.

3 Comments

I tested in the 64-bit system and getting this issue.
Yes, I print PHP version architecture 32 bit and getting 2147483647 but when I tested in 64 bit then getting actual value 4024352118. So you are correct and thanks for solving this issue.
you can add your php version? and if it 32 bit version or 64 bit? also the OS version and the cpu?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.