The hex2bin() function decodes a hexadecimal string back into raw binary bytes stored in a PHP string. It is the reverse of bin2hex()—not a tool for converting hex numbers to binary notation.
01
Hex Decode
Hex → raw bytes.
02
Even Length
Two digits per byte.
03
false on Fail
Invalid input check.
04
bin2hex Pair
Encode / decode.
05
Not base_convert
Data vs numbers.
06
Crypto / Debug
Keys, hashes, dumps.
Fundamentals
Definition and Usage
In PHP, hex2bin() takes a string of hexadecimal characters and returns the original binary data. Each pair of hex digits represents one byte: 48 is the byte for ASCII H, 65 is e, and so on. The result is a normal PHP string holding raw bytes—which may or may not be readable text.
💡
Beginner Tip
Do not confuse hex2bin() with converting the number 255 to binary digits 11111111. For numeric radix conversion, use base_convert(). Use hex2bin() when you have hex-encoded byte data to decode.
Foundation
📝 Syntax
The function accepts one hex-encoded string and returns decoded bytes or false:
PHP
string|false hex2bin(string $string)
Parameters
$string — hexadecimal representation of data. Valid characters: 0–9, a–f, A–F. Length must be even.
Return Value
Returns the binary string on success, or false if the input has odd length or contains invalid hex characters. PHP emits an E_WARNING on failure.
Cheat Sheet
⚡ Quick Reference
Decode hex
hex2bin($hex)
Hex → bytes
Roundtrip
hex2bin(bin2hex($s))
Restore original
Safe check
$b = hex2bin($hex);
if ($b === false) { ... }
Handle failure
Numbers
base_convert($n, 16, 2)
Not hex2bin()
Hands-On
Examples Gallery
Run these examples in PHP 5.4+. Each demonstrates a common pattern beginners encounter with hex2bin().
📚 Getting Started
Decode a hex string that represents readable ASCII text.
Example 1 — Decode Hex to a Text String
Each pair of hex digits maps to one ASCII character in the decoded output.
Result: false
Success: no
Warning: hex2bin(): Hexadecimal input string must have an even length
How It Works
Each byte needs two hex digits. An odd-length string cannot be decoded completely, so PHP returns false and raises a warning. Always compare with === false.
Example 4 — hex2bin() vs base_convert()
These functions solve different problems—a common beginner mistake is mixing them up.
PHP
<?php
// hex2bin: decode hex-encoded BYTE data
$bytes = hex2bin("ff00"); // 2 raw bytes
echo "hex2bin length: " . strlen($bytes) . "\n";
// base_convert: convert a NUMBER between bases
$digits = base_convert("ff", 16, 2); // "11111111"
echo "base_convert: $digits\n";
?>
📤 Output:
hex2bin length: 2
base_convert: 11111111
How It Works
hex2bin("ff00") produces two binary bytes (255 and 0). base_convert("ff", 16, 2) produces the string"11111111" representing the number 255 in binary notation—completely different semantics.
Example 5 — Decode Non-Text Binary Data
Not every decoded result is readable text—crypto keys, hashes, and file chunks are often stored as hex.
Input must contain only valid hex digits with an even character count.
Input
2
PHP validates and pairs digits
Each pair (e.g. 48) maps to one byte (72). Invalid or odd-length input returns false.
Transform
3
Binary string is built
Bytes are concatenated into a PHP string. Length equals half the hex string length.
Output
=
💾
Raw byte string
Ready for crypto, file I/O, or further string processing.
Important
📝 Notes
Available since PHP 5.4.0. On older versions use pack('H*', $hex).
Not the same as base_convert() for numeric radix conversion (see Example 4).
Input length must be even; odd length returns false with a warning.
Invalid characters (e.g. zz) also return false.
Always check for false before using the decoded result.
Wrap Up
Conclusion
The hex2bin() function decodes hex-encoded byte data back into a PHP binary string. Pair it with bin2hex() for encoding, validate return values, and use base_convert() when you need numeric base conversion instead.
Whether you are restoring a debug dump, parsing a protocol field, or loading a crypto key, hex2bin() is the standard PHP tool for hex-to-bytes decoding.
Check for false with strict comparison (=== false)
Use with bin2hex() for encode/decode roundtrips
Validate hex input length is even before calling
Use base_convert() for numeric base changes
Inspect non-text bytes with ord() and strlen()
❌ Don’t
Confuse hex2bin with hex-to-binary number conversion
Assume decoded output is always readable text
Ignore warnings on invalid hex input
Pass hex strings with spaces or 0x prefixes without stripping them
Display raw binary strings directly in HTML without encoding
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about hex2bin()
Use these points whenever you decode hex strings in PHP.
5
Core concepts
💾01
Hex → Bytes
Decode byte data.
Purpose
📝02
Even Length
Two digits per byte.
Syntax
⚠03
Returns false
On invalid input.
Errors
🔄04
bin2hex Pair
Inverse encode.
Related
🔢05
Not base_convert
Data vs numbers.
Distinction
❓ Frequently Asked Questions
hex2bin() decodes a hexadecimal string into its original binary form (returned as a PHP string of raw bytes). Each pair of hex digits becomes one byte—for example, "48656c6c6f" becomes the bytes that spell "Hello".
string|false hex2bin(string $string). Pass a hex-encoded string with an even number of characters (0–9, a–f, A–F). Returns the decoded binary string on success, or false on failure.
No. hex2bin() decodes hex-encoded byte data, not numeric radix conversion. To convert the number 255 from hex to binary digits, use base_convert('ff', 16, 2)—not hex2bin().
hex2bin() returns false and PHP emits an E_WARNING because each byte requires exactly two hex digits. An input like "414" (three characters) is invalid.
They are inverse operations. bin2hex() encodes raw bytes as hex; hex2bin() decodes hex back to bytes. Together they are useful for debugging, serialization, and crypto workflows.
hex2bin() is available in PHP 5.4.0 and later (including PHP 7 and 8). On PHP versions before 5.4, use pack('H*', $hex) as an alternative.