Home  >  Article  >  Backend Development  >  How to convert data to hexadecimal in php

How to convert data to hexadecimal in php

青灯夜游
青灯夜游Original
2021-12-02 18:31:436552browse

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");
?>

Output result:

1e
a
633
46

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);
?>

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);
?>

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!

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