Home>Article>Backend Development> How to convert decimal to hexadecimal in PHP? (code example)

How to convert decimal to hexadecimal in PHP? (code example)

青灯夜游
青灯夜游 Original
2019-04-11 10:11:36 12418browse

In PHP, you can convert decimal to hexadecimal by using the built-in function dechex() or base_convert() function. The following article will introduce to you how the dechex() and base_convert() functions convert decimal to hexadecimal. I hope it will be helpful to you. [Video tutorial recommendation:PHP tutorial]

How to convert decimal to hexadecimal in PHP? (code example)

Method 1: Use the dechex() function to convert decimal to hexadecimal

The dechex() function is a built-in function in PHP that is used to convert a given decimal number into its equivalent hexadecimal number. The word "dechex" in the function name stands for decimal to hexadecimal.

Note:

● The dechex() function only applies to unsigned numbers. If the argument passed to it is negative, then it will treat it as an unsigned number.

●The maximum number that can be converted is 4294967295 (decimal), and the result is "ffffffff".

Example:Convert decimal to hexadecimal

"; echo "十进制 ".$num2." 转换成十六进制 ".dechex($num2)."
"; echo "十进制 ".$num3." 转换成十六进制 ".dechex($num3)."
"; ?>

Output:

How to convert decimal to hexadecimal in PHP? (code example)

Method 2: Use the base_convert() function to convert decimal to hexadecimal

The base_convert() function can convert numbers between any bases.

Example 1:Convert decimal to hexadecimal

"; echo "十进制 ".$num2." 转换成十六进制 ".base_convert($num2,10,16)."
"; ?>

Output:

十进制 10 转换成十六进制 a 十进制 47 转换成十六进制 2f

Example 2:Convert decimal Convert hexadecimal to decimal

"; ?>

Output:

十六进制 16e7 转换成十进制 5863

Example 3:Convert octal to decimal

"; ?>

Output:

八进制 0031 转换成十进制 25

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to convert decimal to hexadecimal in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn