0

Im trying to do a php multiplication of two 32bit long hexadecimal valuey with PHP and it seems it is messing up this calculation, the same happens if i multiplicate it as decimal value.

The calculation is as example: 0xB5365D09 * 0xDEBC252C

Converting to decimal before with hexdec doesnt change anything.

The expected result should be 0x9DAA52EA21664A8C but PHPs result is 0x9DAA52EA21664800

Example:

<?php
$res = 0xB5365D09 * 0xDEBC252C;
echo dechex(intval($res));
?>

What i am doing wrong here?

PHP8.2 running on debian, 64bit AMD.

C.E.
  • 664
  • 2
  • 5
  • 21
  • You're not showing any code. We cannot tell what you're doing wrong without the code. – KIKO Software Dec 12 '22 at 14:16
  • The code is simply what it should be: $res = 0xAA * 0xBB; – C.E. Dec 12 '22 at 14:17
  • That seems to work. See: https://3v4l.org/eZpe1 – KIKO Software Dec 12 '22 at 14:19
  • 1
    Please add a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) to your question. – KIKO Software Dec 12 '22 at 14:20
  • I added the code in my question and here you can see it live: https://3v4l.org/Mn2lX – C.E. Dec 12 '22 at 14:33
  • 2
    `0x9DAA52EA21664A8C` is `11360984175531674252` in decimal and `9223372036854775807` is the current `PHP_INT_MAX`. You could try [`bcmul`](https://www.php.net/manual/en/function.bcmul.php) or similar – Chris Haas Dec 12 '22 at 14:42
  • Thanks. I tried with gmp_mul now and its working but its still a strange behaviour of PHP, i think it should at least drop an error there. – C.E. Dec 12 '22 at 14:54
  • 1
    @C.E., I don't speak for the PHP core team, but I believe that the general idea is most people know if they are working with big numbers and would hopefully be aware of overflows (I think it follows the rules of C), and this is also a relatively small group of people, so adding overflow checks everywhere was deemed overkill. There was an [attempt at adding arbitrarily-sized integers](https://wiki.php.net/rfc/bigint) into PHP but that stalled out. – Chris Haas Dec 12 '22 at 15:21

2 Answers2

1

So, for others to find the answer: Someone stated in the comments to my question, that the result is above the PHP_MAX_INT limit. So when PHP handle it as a FLOAT, there will be some precision of the result lost. I got it to work using bcmath. In my case, i didnt do math with the result any further so i grabbed some piece of code from here, and made a simple function which does what i need. Here you can see a minimum-example:

function bcmul_hex($h1, $h2) {
    $dec = bcmul($h1, $h2);
    $hex = '';
    do {    
        $last = bcmod($dec, 16);
        $hex = dechex($last).$hex;
        $dec = bcdiv(bcsub($dec, $last), 16);
    } while($dec>0);
    return $hex;
}

echo bcmul_hex(0xB5365D09, 0xDEBC252C);

Here is a live example.

C.E.
  • 664
  • 2
  • 5
  • 21
-1

If you're trying to perform a mathematical operation on a hexadecimal value in PHP, you can use the hexdec function to convert the hexadecimal value to a decimal value, perform the operation, and then use the dechex function to convert the result back to a hexadecimal value.

Here's an example:

   <?php

$hex1 = "B5365D09";
$hex2 = "DEBC252C";

// Convert hexadecimal values to decimal
$dec1 = hexdec($hex1);
$dec2 = hexdec($hex2);

// Perform multiplication operation
$result = $dec1 * $dec2;

// Convert result back to hexadecimal
$hex_result = dechex($result);

// Print result
echo "Result: $hex_result\n";

This code will output the result of the multiplication as a hexadecimal value.

  • 1
    When I plug this code in I get a [fatal error](https://3v4l.org/pPqhU). When I cast the [float to an int](https://3v4l.org/Fc1aP), it fixes that error, however this results in the same number as the OP was getting. – Chris Haas Dec 12 '22 at 14:47
  • Thanks for your answer. The multiplication in this case will return a float value so its neccesary to add an intval($result) or declaration for dechex to work. However, as i mentioned before in my question converting to decimal first doesnt work. See here: https://3v4l.org/aJEVM – C.E. Dec 12 '22 at 14:49