bin2hex
Converts binary data into its lowercase hexadecimal string representation, two characters per byte.
bin2hex(string $string): string- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | The binary data to convert. |
Return value
stringThe hexadecimal representation of the given data, using lowercase letters, exactly twice as long as the input.
bin2hex turns each byte of a string into two lowercase hex digits. It never fails, never adds separators, and always returns a string exactly twice the input length. An empty string in gives an empty string out.
When to reach for it
Reach for it whenever raw bytes need to survive somewhere that only accepts printable ASCII: a database column, a URL, a log line, a JSON payload. It is the standard way to render the raw output of random_bytes, hash(..., true) or openssl_random_pseudo_bytes as a token or fingerprint. hex2bin reverses it exactly.
What trips people up
It converts bytes, not numbers. bin2hex("255") is 323535 — the hex codes of the characters 2, 5, 5 — not ff. To convert an integer to hex use dechex; to convert between arbitrary bases use base_convert.
The output is always lowercase; wrap it in strtoupper if a protocol demands uppercase, and remember to lowercase again before comparing. Also note that hex doubles your storage: 32 random bytes become a 64-character token.
Example
When to use it
- Turn random_bytes output into a printable API key, session token or password-reset code.
- Store or log the raw binary digest from hash($algo, $data, true) as a readable checksum.
- Inspect invisible characters — BOMs, NUL bytes, stray \r — in a string that looks fine when echoed.
Frequently asked questions
What is the difference between bin2hex and dechex?
bin2hex encodes every byte of a string as two hex digits; dechex converts a single integer to a hex string. bin2hex("255") is "323535" while dechex(255) is "ff".
Does bin2hex produce uppercase or lowercase hex?
Always lowercase. Apply strtoupper if a protocol requires uppercase, and normalise case before comparing two hex strings.
How do I convert the hex string back to binary?
Use hex2bin, which is the exact inverse. It returns false and emits a warning if the string has an odd length or contains non-hex characters.
More examples
Rendering a raw hash digest
The raw digest is 32 unprintable bytes. bin2hex renders it as 64 hex characters, which is byte-for-byte the same string hash() returns when you leave the binary flag off — proof that the hex form is just an encoding of the same data.
<?php
$digest = hash("sha256", "hello", true);
echo strlen($digest), "\n";
echo bin2hex($digest), "\n";
echo hash("sha256", "hello"), "\n";Bytes versus numbers
The first line hexes the three characters "2", "5", "5" and prints 323535. dechex(255) prints ff, the number. chr(255) makes a single byte, so bin2hex gives ff as well. The last line shows how to force uppercase output.
<?php
echo bin2hex("255"), "\n";
echo dechex(255), "\n";
echo bin2hex(chr(255)), "\n";
echo strtoupper(bin2hex("\x01\x02\x0a")), "\n";Finding invisible characters
Echoing the line looks normal, but the hex dump ends in 0d0a, revealing a Windows line ending. The second dump starts with efbbbf, a UTF-8 byte order mark that would otherwise corrupt the first CSV column name.
<?php
$line = "name,value\r\n";
$bom = "\xef\xbb\xbfid";
echo $line;
echo bin2hex($line), "\n";
echo bin2hex($bom), "\n";
var_dump(str_starts_with($bom, "\xef\xbb\xbf"));
Discussion