Search code examples
phphex

How can I convert an hexadecimal string to float?


I have a hexadecimal string, "0x77b1774c931ea947dec707c71653099f", I need to convert to 1.590995877159802e+38.

In JavaScript I used the following code.

eval("(0x77b1774c931ea947dec707c71653099f)");

Which code should I use in PHP?


Solution

  • You can use the hexdec() function, just pass the hex to it like this:

    echo hexdec("0x77b1774c931ea947dec707c71653099f"); // Result: 1.5909958771598E+38
    

    This function will also convert numbers that are too big for an int to float

    Documentation