Home>Article>Backend Development> How to convert data into hexadecimal string in php
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)".
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:
"; echo dechex("10") . "
"; echo dechex("1587") . "
"; echo dechex("70"); ?>
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 parametertobase
is 16, other base numbers can be converted into hexadecimal numbers.
Example:
"; echo base_convert("364", 8, 16) . "
"; ?>
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:
"; echo base_convert("a", 16, 10) . "
"; echo base_convert("11ff", 16, 10) . "
"; echo base_convert("cceeff", 16, 10); ?>
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.
"; echo hexdec("a") . "
"; echo hexdec("11ff") . "
"; echo hexdec("cceeff"); ?>
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.
"; echo bin2hex("123")."
"; echo bin2hex("10")."
"; ?>
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!