Large hex values with PHP hexdec

前端 未结 7 1267
清歌不尽
清歌不尽 2020-12-03 08:23

I have some large HEX values that I want to display as regular numbers, I was using hexdec() to convert to float, and I found a function on PHP.net to convert that to decima

7条回答
  •  抹茶落季
    2020-12-03 08:42

    Run into this issue while storing 64-bit keys in MySQL database. I was able to get a bit perfect conversion to a 64-bit signed integer (PHP limitation) using a few binary operators: (This code is 16x faster than bchexdec function and resulting variables are using half the memory on average).

    function x64toSignedInt($k){
        $left = hexdec(substr($k,0,8));
        $right = hexdec(substr($k,8,8));
        return (int) ($left << 32) | $right;
    }
    

    MySQL signed BIGINT datatype is a great match for this as an index or storage in general. HEX(column) is a simple way to convert it back to HEX within the SQL query for use elsewhere.

提交回复
热议问题