How to Implement the conversion from a negative number to a decimal number in PHP16 notation

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.