Home > Backend Development > PHP Problem > How to convert hexadecimal and decimal to each other in php

How to convert hexadecimal and decimal to each other in php

青灯夜游
Release: 2023-03-14 17:26:02
Original
3851 people have browsed it

Methods for mutual conversion: 1. Use the "hexdec (hexadecimal string)" statement to convert hexadecimal to decimal; 2. Use the "dechex (decimal value)" statement , can convert decimal to hexadecimal value; 3. Use the "base_convert("base value", original base, target base)" statement.

How to convert hexadecimal and decimal to each other in php

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

16 hexadecimal and Conversion between decimal numbers

1. To convert hexadecimal numbers to decimal numbers

, you can use hexdec(hexdec) Hexadecimal string) function, which can convert hexadecimal numbers into decimal numbers.

<?php
echo hexdec("1e") . "<br>";
echo hexdec("a") . "<br>";
echo hexdec("11ff") . "<br>";
echo hexdec("cceeff");
?>
Copy after login

Output result:

How to convert hexadecimal and decimal to each other in php

You can also use the base_convert() function, just set "bindec(hexadecimal string, 16 , 10)" is enough.

<?php
echo base_convert("1e", 16, 10) . "<br>";
echo base_convert("a", 16, 10) . "<br>";
echo base_convert("11ff", 16, 10) . "<br>";
echo base_convert("cceeff", 16, 10);
?>
Copy after login

Output result:

30
10
4607
13430527
Copy after login

2. Convert decimal number to hexadecimal value

You can use dechex (decimal value) Function, which converts decimal numbers to hexadecimal numbers.

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

Output result:

How to convert hexadecimal and decimal to each other in php

You can also use the base_convert() function, just set "bindec(decimal value, 10, 16)" is all.

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

Output results:

1e
a
633
46
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert hexadecimal and decimal to each other 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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template