php functions to convert to hexadecimal include: 1. bin2hex(), which can convert a string of ASCII characters into a hexadecimal value; 2. dechex(), which can convert a decimal number into a hexadecimal value. Base number, 3, base_convert(), can convert numbers between any bases.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php to 16 System function
1, bin2hex() function
bin2hex() function converts a string of ASCII characters into a hexadecimal value .
Syntax: bin2hex(string)
Return value: Returns the hexadecimal value of the string to be converted.
Example:
<?php $str = bin2hex("Hello!"); echo($str); ?>
2. dechex() function
dechex() function converts decimal numbers is a hexadecimal number.
Syntax: 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"); ?>
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.
3. base_convert() function
base_convert() function converts numbers between arbitrary bases.
Syntax: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>"; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the functions for converting php to hexadecimal?. For more information, please follow other related articles on the PHP Chinese website!