How to convert decimal to hexadecimal in php

青灯夜游
Release: 2023-03-14 11:44:01
Original
4015 people have browsed it

php method to convert decimal to hexadecimal: 1. Use the dechex() function, the syntax "dechex($number)"; 2. Use the base_convert() function, the syntax "base_convert($number, 10,16)”.

How to convert decimal to hexadecimal in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php converts decimal into hexadecimal

1. Use the dechex() function

dechex($number) function to convert decimal Number $number is converted into a hexadecimal number.

<?php
echo dechex("30") . "<br>";
echo dechex("10") . "<br>";
echo dechex("1587") . "<br>";
echo dechex("70");
?>
Copy after login

How to convert decimal to hexadecimal in php

#2. Use the base_convert() function

base_convert() function to convert numbers between arbitrary bases.

base_convert(number,frombase,tobase);
Copy after login
  • number Required. Specifies the number to be converted.​

  • #frombase Required. Specifies the original base of the number. Between 2 and 36 (inclusive). Numbers above decimal are represented by the letters a-z, such as a for 10, b for 11, and z for 35.​

  • #tobase Required. Specifies the base to be converted. Between 2 and 36 (inclusive). Numbers above decimal are represented by the letters a-z, such as a for 10, b for 11, and z for 35.

<?php
header("content-type:text/html;charset=utf-8");
$num1=10;
$num2=47;
echo "十进制 ".$num1." 转换成十六进制 ".base_convert($num1,10,16)."<br>"; 
echo "十进制 ".$num2." 转换成十六进制 ".base_convert($num2,10,16)."<br>"; 
?>
Copy after login

How to convert decimal to hexadecimal in php

Recommended study: "PHP Video Tutorial"

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template