Take the 2-minute tour ×

I'm pretty inexperienced in PHP so this may be obvious to some of you, but if I call md5($mystring,true) in PHP it says it returns a "raw binary format with a length of 16". So what is that? Is it an array? An array of what? How do I read the individual bits and bytes of that return value?

None of the examples I can find online use it without going straight into base64_encode() or something. I just want to be able to check the fifth bit, or the third byte, for example.

share|improve this question

5 Answers 5

up vote 1 down vote accepted

It returns it as a "string" with each Character being a byte of the output. What you might consider instead is using the hex output of md5() without the second parameter and converting substrings of it to numbers using intval() with 16 as the base parameter. Like so:

$hash = md5($raw);
$byte = intval(substr($hash, 0, 2), 16);
share|improve this answer
var_dump(md5("string", TRUE));

"Raw binary format" means a string (as strings are binary-safe in PHP):

string(16) "�\����= �(��^{!"

If you want to read a byte out of that, use the $string[5] offset syntax for that. Or to extract a single bit out of the fifth byte for example:

$bit4_from_5th_byte = ord($result[5]) & (1 << 4);
share|improve this answer
It’s notable to say that the displayed output depends on the character encoding (in this case UTF-8). –  Gumbo Oct 18 '11 at 17:39

An MD5 hash is a 128bit number, e.g. 16 bytes of raw binary data. For legibility, PHP's md5() function defaults to outputting this as a human readable string, where those 16 binary bytes get converted into a 32 character alpha-numeric string.

When you specify that second true parameter for MD5, you're telling PHP to return that raw 128bit number instead of the human readable version.

share|improve this answer
I think that get converted into a 32 character is really get converted into a 16 character as it's converted to hexadecimal. –  PhoneixS Dec 18 at 10:09

To get the individual bits:

$md5 = md5( "b", true );
$l = strlen( $md5 );
$bits = "";

    for( $i = 0; $i < $l; ++$i ) {
    $num = ord( $md5[$i] );
        for( $j = 7; $j >= 0; --$j ) {
        $bits .= ( $num & ( 1 << $j ) ) ? "1" : "0";


echo $bits."\n";


echo asciibin2hex( $bits ) == md5("b"); // true

So we go byte by byte, and since it is in string form, we need to convert it to ASCII number by ord(). Now go through the byte, bit by bit and see which are turned on and which are turned off and concatenate to the bit string. Go to next byte and rinse and repeat until all 128 bits are read.

Read third bit ( from the left ) in third byte:

ord( $md5[2] ) & ( 1 << ( 8 - 3 ) )

returns 1 if the bit is turned on, 0 otherwise

share|improve this answer

The RAW output of MD5 is the plain binary string of the hash. You cannot print it to the screen since it's not encoded as actual ASCII characters but binary numbers. This is only useful if you need to store or transfer the hash in a binary format.

share|improve this answer

Your Answer


