Home > Article > Backend Development > PHP function hex2bin() that converts a string of hexadecimal values into ASCII characters
Example
Convert hexadecimal value to ASCII character:
<?php echo hex2bin("48656c6c6f20576f726c6421"); ?>
Output result of the above example:
Hello World!
Definition and usage
hex2bin () function converts a string of hexadecimal values into ASCII characters.
Syntax
hex2bin(string)
Parameters Description
string Required. The hexadecimal value to convert.
Technical details
Return value: Returns the ASCII characters of the converted string, or FALSE if it fails.
PHP Version: 5.4.0+
Update Log: Since PHP 5.4.1, if the string length is odd, a warning is thrown. In PHP 5.4.0, odd strings are silently accepted, but the last byte is removed.
Since PHP 5.5.1, a warning is thrown if the string is an invalid hexadecimal string.
There is a bin2hex method in php, but there is no hex2bin method. The following is a simple implementation of hex2bin:
<?php function hex2bin($data) { $len = strlen($data); return pack("H" . $len, $data); } ?>
The above is the detailed content of PHP function hex2bin() that converts a string of hexadecimal values into ASCII characters. For more information, please follow other related articles on the PHP Chinese website!