For example, $ a-101; the PHP built-in function can convert $ a into a hexadecimal echodechex (-101 & amp; 0xff); ----------- output result: 9b please explain, how does php convert 9b, such a negative hexadecimal number, back to the 10-hexadecimal-101 example?
$ A =-101;
You can convert $ a to hexadecimal using PHP built-in functions.
Echo dechex (-101 & 0xff );
-----------
Output result: 9b
How can php convert the negative hexadecimal number of 9b to-101 in decimal order?
Reply content:
For example
$ A =-101;
You can convert $ a to hexadecimal using PHP built-in functions.
Echo dechex (-101 & 0xff );
-----------
Output result: 9b
How can php convert the negative hexadecimal number of 9b to-101 in decimal order?
-101 decbin displays 32 bits. & 0xff only takes the lowest 8 bits, and all the high bits are intercepted. You cannot restore them.
In the case of-101, this can be done.
$signed = unpack("l", pack("l", hexdec("ffffff9b")));var_dump(reset($signed));
Hexdec-hexadecimal conversion to decimal
Description
Number hexdec (string $ hex_string)
Returns the decimal number equivalent to the hexadecimal number indicated by the hex_string parameter. Hexdec () converts a hexadecimal string to a decimal number.
Hexdec () ignores any non-hexadecimal characters it encounters.
Parameters
Hex_string
Hexadecimal string to be converted
Return Value
Hex_string decimal representation