PHP bin2hex() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

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.

PHP
$hex = bin2hex($data);
CallWhat it does
bin2hex("Hi")Returns 4869.
hex2bin($hex)Restores the original bytes.

Output length is twice the input byte length.

Library declarations
PHP
bin2hex(string $string): string

Parameters

ParameterDescription
stringRequired. The binary or text data to convert.

Return Value

Return ValueDescription
stringA hexadecimal representation of the input.

PHP Examples

Example 1 — Encode a Simple String

Convert ABC to hex. Each letter becomes two digits.

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

How It Works

  1. Read bytes. A, B, and C are three bytes.
  2. Map to hex. 65 → 41, 66 → 42, 67 → 43.
  3. Join. Digits concatenate with no separators: 414243.
  4. Case. Letters in hex are always lowercase (a–f).

Example 2 — Encode, Then Decode

Round-trip with hex2bin() 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 pairs of digits back into bytes.
  3. Match. $back equals "Hello".
  4. 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
<?php
$str = "Hi";
$hex = bin2hex($str);

echo strlen($str) . "\n";  // 2
echo strlen($hex) . "\n";  // 4
echo $hex;                 // 4869

How It Works

  1. Input length. "Hi" is 2 bytes.
  2. Encode. H → 48, i → 69.
  3. Output length. 4 hex characters = 2 × 2.
  4. Rule. Always expect strlen(bin2hex($s)) === 2 * strlen($s).

Frequently Asked Questions

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.
string bin2hex(string $string). You pass a string (PHP strings hold raw bytes) and receive a hex-encoded string. The original is not modified.
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".
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.
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.

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