What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.



bin2hex — Converts a string value from binary to hexadecimal.


string bin2hex(string);
string string: String to convert from binary to hex


Hexadecimal value.


bin2hex() converts a string of ASCII characters to their corresponding hexadecimal values. Note that any value passed to the function is converted to an ASCII string (if possible). The string can be converted back using pack() . See the examples.


PHP 3.0.9+, PHP 4+

See also

Other functions that deal with the ASCII value of a character:








Example 1188. Demonstrate how bin2hex() converts a string

$string = "I'm a lumberjack and I'm ok...\n";

// Convert a string to its hex representation
$hex_string = bin2hex($string);
echo $hex_string, "\n";

// Convert the string back to binary
echo pack('H*', $hex_string);

echo "\n";

echo "Here is a character-by-character breakdown of how the hex values
correspond with character values:\n\n";

// Show more clearly how bin2hex() converts strings
// Loop through the converted string two characters at a time
for ($ndx = 0; $ndx < strlen($hex_string); $ndx += 2) {

    // Grab the two hex vales that represent a single character
    $hex_chunk = $hex_string[$ndx].$hex_string[$ndx+1];

    // Show each chunk of the string, along with the character it represents
    printf("Hex: %s Char: %s\n", $hex_chunk, pack('H2', $hex_chunk));

I'm a lumberjack and I'm ok...

Here is a character-by-character breakdown of how the hex values
correspond with character values:

Hex: 49 Char: I
Hex: 27 Char: '
Hex: 6d Char: m
Hex: 20 Char:
Hex: 61 Char: a
Hex: 20 Char:
Hex: 6c Char: l
Hex: 75 Char: u
Hex: 6d Char: m
Hex: 62 Char: b
Hex: 65 Char: e
Hex: 72 Char: r
Hex: 6a Char: j
Hex: 61 Char: a
Hex: 63 Char: c
Hex: 6b Char: k
Hex: 20 Char:
Hex: 61 Char: a
Hex: 6e Char: n
Hex: 64 Char: d
Hex: 20 Char:
Hex: 49 Char: I
Hex: 27 Char: '
Hex: 6d Char: m
Hex: 20 Char:
Hex: 6f Char: o
Hex: 6b Char: k
Hex: 2e Char: .
Hex: 2e Char: .
Hex: 2e Char: .
Hex: 0a Char:


Example 1189. Show how bin2hex() deals with non-character data

// Show how bin2hex() handles non-strings
echo "bin2hex('1') returns: " . bin2hex('1') . "\n";

// It converts non-character data to an ASCII string
echo "bin2hex(1) returns: " . bin2hex(1) . "\n";

// Can you tell the difference?
// To make bin2hex() show the hex representation of 1, use an octal escape sequence
echo 'bin2hex("\1") returns: ' . bin2hex("\1") . "\n";

// Try converting a character outside the range of the ASCII character table
echo 'bin2hex("\400") returns: ' . bin2hex("\400") . "\n";

bin2hex('1') returns: 31
bin2hex(1) returns: 31
bin2hex("\1") returns: 01
bin2hex("\400") returns: 00

PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at The authors of this book have elected not to choose any options under the OPL. This online book was obtained from and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.