PHP hex2bin() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, hex2bin() converts a hexadecimal string back into binary data.

You pass one hex string. It is required and must have an even length. Use bin2hex() for the reverse.

Syntax

Call hex2bin on the hex text. Start with this line.

PHP
$bin = hex2bin($hex);
CallWhat it does
hex2bin("4869")Returns "Hi".
bin2hex($bin)Converts binary back to hex.

Odd-length input or invalid hex digits cause warnings/false in older styles—validate first.

Library declarations
PHP
hex2bin(string $string): string|false

Parameters

ParameterDescription
stringRequired. Hexadecimal text with an even number of digits.

Return Value

Return ValueDescription
string|falseBinary data, or false on failure.

PHP Examples

Example 1 — Decode a Hex String

Turn 414243 into the letters ABC.

PHP
<?php
echo hex2bin("414243");
// ABC

How It Works

  1. Pairs. Split as 41, 42, 43.
  2. Map. Those hex values are ASCII for A, B, C.
  3. Join. The bytes form the string ABC.
  4. Case. Digits may be lowercase or uppercase (a–f / A–F).

Example 2 — Encode, Then Decode

Round-trip with bin2hex() restores the original string.

PHP
<?php
$original = "Hello";
$hex = bin2hex($original);
$back = hex2bin($hex);

echo $hex . "\n";
echo $back . "\n";
// 48656c6c6f
// Hello

How It Works

  1. Encode. bin2hex("Hello") → 48656c6c6f.
  2. Decode. hex2bin($hex) turns each pair back into a byte.
  3. Match. $back equals "Hello".
  4. Length. Hex is twice as long; decoding halves it again.

Example 3 — Odd Length Returns false

Always check the return value before treating it as a string.

PHP
<?php
$result = @hex2bin("414"); // odd length

if ($result === false) {
    echo "decode failed";
} else {
    echo $result;
}
// decode failed

How It Works

  1. Odd length. "414" has three characters—not complete pairs.
  2. Failure. hex2bin() returns false (and warns).
  3. Strict check. Use === false so empty binary is not confused with failure.
  4. Fix. Pad or reject incomplete hex before decoding.

Frequently Asked Questions

hex2bin() decodes a hexadecimal string into its original binary form (a PHP string of raw bytes). Each pair of hex digits becomes one byte—for example, "48656c6c6f" becomes "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 turn the number 0xff into binary digits, use base_convert('ff', 16, 2)—not hex2bin().
hex2bin() returns false and PHP emits a warning, because each byte needs 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. A round-trip restores the original string.

Next: html_entity_decode()

Learn how to convert HTML entities back into characters.

html_entity_decode tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful