How to convert data to hexadecimal in php

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

Conversion method: 1. Use dechex() to convert decimal to hexadecimal, the syntax is "dechex (decimal value)"; 2. Use base_convert(), the syntax is "bindec(decimal value, 10 ,16)"; 3. Use bin2hex(), the syntax is "bin2hex (string)".

How to convert data to hexadecimal in php

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

php converts data Is hexadecimal

1. Use the dechex() function - you can convert decimal to hexadecimal

dechex (decimal value) can convert decimal numbers into hexadecimal numbers.

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

Output result:

1e
a
633
46
Copy after login

2. Use the base_convert() function--to convert decimal to hexadecimal

base_convert() Function converts numbers between arbitrary bases.

Set "bindec(decimal value, 10, 16)" to convert decimal to hexadecimal.

<?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 result:

How to convert data to hexadecimal in php

3. Use the bin2hex() function--convert the string to hexadecimal

bin2hex(string) Function converts a string of ASCII characters into a hexadecimal value.

<?php
$str = bin2hex("Hello World!");
echo($str);
?>
Copy after login

How to convert data to hexadecimal in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert data 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