Home > Backend Development > PHP Tutorial > PHP function hex2bin() that converts a string of hexadecimal values ​​into ASCII characters

PHP function hex2bin() that converts a string of hexadecimal values ​​into ASCII characters

黄舟
Release: 2023-03-16 22:04:02
Original
3255 people have browsed it

Example

Convert hexadecimal value to ASCII character:

<?php
echo hex2bin("48656c6c6f20576f726c6421");
?>
Copy after login

Output result of the above example:

Hello World!
Copy after login

Definition and usage

hex2bin () function converts a string of hexadecimal values ​​into ASCII characters.

Syntax

hex2bin(string)
Copy after login

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); } 
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template