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

How to convert data into hexadecimal string in php

青灯夜游
青灯夜游Original
2022-08-25 18:28:013273browse

3 conversion methods: 1. Use the dechex() function to convert decimal numbers into hexadecimal strings, the syntax is "dechex (specified data value);". 2. Use the base_convert() function to convert any base value into a hexadecimal string. The syntax is "base_convert(data value, original base, 16);". 3. Use the bin2hex() function to convert a string of ASCII characters into a hexadecimal string, with the syntax "bin2hex (data value)".

How to convert data into hexadecimal string in php

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

In php, transfer the data There are two situations for hexadecimal strings:

  • Convert other hexadecimal data to hexadecimal strings

  • Convert ASCII character string to hexadecimal string

Different functions are used in different situations.

Case 1: Convert other base data to hexadecimal string

PHP provides two functions for conversion:

  • dechex() function

  • base_convert() function

1, dechex( )Function conversion

dechex() function converts decimal numbers to hexadecimal numbers.

dechex(number);

Return value: a string containing a hexadecimal number with a decimal value.

Example:

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

How to convert data into hexadecimal string in php

Description:

hexdec() Converts a hexadecimal string to a decimal number. The maximum value that can be converted is 7ffffffff, which is 2147483647 in decimal. Starting with PHP 4.1.0, this function can handle large numbers, in which case it returns a float type.

hexdec() Replaces all non-hexadecimal characters encountered with 0. This way, all zeros on the left are ignored, but zeros on the right are included in the value.

2. base_convert() function conversion

base_convert() function converts numbers between arbitrary bases.

base_convert(number,frombase,tobase);
Parameters Description
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.

When the value of parameter tobase is 16, other base numbers can be converted into hexadecimal numbers.

Example:

<?php
echo base_convert("30", 10, 16) . "<br>";
echo base_convert("364", 8, 16) . "<br>";
?>

How to convert data into hexadecimal string in php

Explanation:

base_convert() function can also convert hexadecimal to other base. At this time, the value of the parameter frombase needs to be a fixed value of 16.

Example 16 to 10:

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

How to convert data into hexadecimal string in php

If you just want to convert 16 to 10, there is a more convenient function hexdec()

hexdec() Converts a hexadecimal string to a decimal number. The maximum value that can be converted is 7ffffffff, which is 2147483647 in decimal. Starting with PHP 4.1.0, this function can handle large numbers, in which case it returns a float type.

hexdec() Replaces all non-hexadecimal characters encountered with 0. This way, all zeros on the left are ignored, but zeros on the right are included in the value.

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

How to convert data into hexadecimal string in php

Case 2: Convert ASCII character string to hexadecimal string

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

bin2hex(string)

Return value: Returns the hexadecimal value of the string to be converted.

<?php
echo bin2hex("Hello!")."<br>";
echo bin2hex("123")."<br>";
echo bin2hex("10")."<br>";
?>

How to convert data into hexadecimal string in php

Recommended learning: "PHP Video Tutorial"

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