Home PHP String Functions bin2hex() PHP bin2hex() Function Definition In PHP, bin2hex() converts binary data into a hexadecimal string. Each byte becomes two hex digits.
You pass one string. It is required. Use hex2bin() to convert the hex text back to binary.
Syntax Call bin2hex on the binary string. Start with this line.
Call What it does bin2hex("Hi")Returns 4869. hex2bin($hex)Restores the original bytes.
Output length is twice the input byte length.
Library declarations bin2hex(string $string): string Parameters Parameter Description string Required. The binary or text data to convert.
Return Value Return Value Description string A hexadecimal representation of the input.
PHP Examples 1. Basic encode 2. Round-trip 3. Length check Example 1 — Encode a Simple String Convert ABC to hex. Each letter becomes two digits.
<?php
echo bin2hex("ABC");
// 414243 How It Works Read bytes. A, B, and C are three bytes.Map to hex. 65 → 41, 66 → 42, 67 → 43.Join. Digits concatenate with no separators: 414243.Case. Letters in hex are always lowercase (a–f).Example 2 — Encode, Then Decode Round-trip with hex2bin() restores the original string.
<?php
$original = "Hello";
$hex = bin2hex($original);
$back = hex2bin($hex);
echo $hex . "\n";
echo $back . "\n";
// 48656c6c6f
// Hello How It Works Encode. bin2hex("Hello") → 48656c6c6f.Decode. hex2bin($hex) turns pairs of digits back into bytes.Match. $back equals "Hello".Odd length. Invalid hex or odd length makes hex2bin() fail—keep pairs complete.Example 3 — Output Is Always Twice as Long Useful when sizing buffers or validating hex payloads.
<?php
$str = "Hi";
$hex = bin2hex($str);
echo strlen($str) . "\n"; // 2
echo strlen($hex) . "\n"; // 4
echo $hex; // 4869 How It Works Input length. "Hi" is 2 bytes.Encode. H → 48, i → 69.Output length. 4 hex characters = 2 × 2.Rule. Always expect strlen(bin2hex($s)) === 2 * strlen($s).Frequently Asked Questions What does PHP bin2hex() do? bin2hex() converts a string of binary data into its hexadecimal representation. Each byte in the input becomes two lowercase hex characters (0–9, a–f) in the output.
What is the syntax for bin2hex()? string bin2hex(string $string). You pass a string (PHP strings hold raw bytes) and receive a hex-encoded string. The original is not modified.
Why is the output twice as long as the input? Each byte (8 bits) is represented by exactly two hexadecimal digits. For example, the byte 255 becomes "ff" and the letter A (byte 65) becomes "41".
How do I convert hex back to binary? Use hex2bin(), the companion function. It decodes a hex string back into its original binary form. hex2bin() returns false if the input contains invalid hex characters or has an odd length.
What is the difference between bin2hex() and dechex()? bin2hex() converts an arbitrary string of bytes to hex. dechex() converts a single integer to its hexadecimal representation. Use bin2hex() for binary data and dechex() for numeric values.
Next: chop() Learn how to trim characters from the end of a string (alias of rtrim).
chop tutorial → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion