hex2bin()
The hex2bin() function is used to convert a hexadecimal string to its binary representation. The syntax of the hex2bin() function is as follows:
The PHP hex2bin() function decodes a string of hexadecimal digits back into the raw bytes they represent. It is the exact inverse of bin2hex(): where bin2hex() turns each byte into two hex characters, hex2bin() reads those character pairs and rebuilds the original byte string. Available since PHP 5.4, its syntax is as follows.
This page covers the syntax, return value, a working decode example, how to handle invalid input safely, and a round-trip with bin2hex().
Syntax
string|false hex2bin ( string $string )The function takes one required parameter, $string, the hexadecimal string to convert. Each pair of hex characters (such as 48) maps to one byte (H). Because two characters make one byte, the input length must be even.
Return value
hex2bin() returns the decoded binary string on success, or false on failure. It fails (and emits a warning) when the input has an odd length or contains characters that are not valid hex digits (0-9, a-f, A-F). Validating the input first — shown below — lets you avoid those warnings.
Here is an example of how to use the hex2bin() function:
Decoding a hex string
In this example, we have a string variable $hex_string containing a hexadecimal string. We use the hex2bin() function to convert the hexadecimal string to its binary representation.
The output of this code will be:
Hello WorldAs you can see, the hex2bin() function has converted the hexadecimal string to its binary representation, which is the string "Hello World".
Validating input before decoding
The hex2bin() function returns false if the input string contains invalid hexadecimal characters. To handle this situation gracefully and avoid warnings, you can validate the input first with ctype_xdigit() (which checks that every character is a hex digit):
In this example, the variable $invalid_hex_string holds an odd-length (and therefore invalid) hexadecimal string. We use ctype_xdigit() to check that the string contains only valid hexadecimal characters before calling hex2bin(). If the validation fails, we display an error message; otherwise, we safely convert the string to binary.
Round-trip with bin2hex()
hex2bin() and bin2hex() are inverses, so encoding a string and then decoding it returns the original value. This is handy when you need to store or transmit binary data as plain ASCII (for example in a URL or a text column) and recover it later:
<?php
$original = "Hello World";
$hex = bin2hex($original); // 48656c6c6f20576f726c64
echo $hex . "\n";
$decoded = hex2bin($hex); // Hello World
echo $decoded . "\n";
var_dump($original === $decoded); // bool(true)
?>The output of this code will be:
48656c6c6f20576f726c64
Hello World
bool(true)Because the decoded value is identical to the original, var_dump() reports bool(true).
When to use it
Use hex2bin() whenever you receive data that was hex-encoded and you need the raw bytes back — common cases include reading hashes or keys stored as hex strings, decoding values produced by bin2hex(), and reconstructing binary payloads (images, encrypted blobs) that were transported as text.
Related functions
bin2hex()— convert a binary string to its hexadecimal representation (the inverse ofhex2bin()).dechex()— convert a decimal integer to a hexadecimal string.hexdec()— convert a hexadecimal string to a decimal integer.