PHP Function Reference

PHP hex2bin() Function



The PHP hex2bin() function is used to convert a string containing hexadecimal representation of data into ASCII characters.

Note: This function does NOT convert a hexadecimal number to a binary number. This can be done using the base_convert() function.

Syntax

hex2bin(str)

Parameters

str Required. Specify string containing hexadecimal representation of data.

Return Value

Returns the ASCII characters representation of the given string or false on failure.

Exceptions

If the hexadecimal input string is of odd length or invalid hexadecimal string an E_WARNING level error is thrown.

Example:

In the example below, hex2bin() function is used to convert the given string containing hexadecimal representation of data into ASCII characters.

<?php
$str = "48656c6c6f20576f726c64";

//converting and printing the hexadecimal 
//data into ASCII characters
echo hex2bin($str);
?>

The output of the above code will be:

Hello World

❮ PHP String Reference