# How To Calculate Merkle Root?

I found this example:

Current merkle hash list: 32650049a0418e4380db0af81788635d8b65424d397170b8499cdc28c4d27006 30861db96905c8dc8b99398ca1cd5bd5b84ac3264a4e1b3e65afa1bcee7540c4

I have tried to calculate the hash using PHP:

``````\$tx1="32650049a0418e4380db0af81788635d8b65424d397170b8499cdc28c4d27006";
\$tx2="30861db96905c8dc8b99398ca1cd5bd5b84ac3264a4e1b3e65afa1bcee7540c4";
\$v=hash('sha256', hash('sha256',\$tx1.\$tx2) );
``````

But I got:

What is wrong?

• you should hash the binary data, not the human-readable hexadecimal representation Oct 16, 2017 at 9:32
• @amaclin Sounds like that should be an answer, not just a comment Oct 17, 2017 at 17:38

This is the correct way to double hash two tx's. First you need to convert them into little endian, and then convert into raw binary data before hashing.

``````<?php

function le_raw (\$str) {
return pack("H*", join("", array_reverse(str_split(\$str, 2))) );
// return map { pack "H*", join "", reverse /(..)/g } @_;
}
function be_hex (\$raw) {
return join("", array_reverse(str_split( unpack("H*", \$raw)[1], 2)));
// return map { join "", reverse unpack("H*", \$_) =~ /(..)/g } @_;
}

\$tx1 = le_raw("32650049a0418e4380db0af81788635d8b65424d397170b8499cdc28c4d27006");
\$tx2 = le_raw("30861db96905c8dc8b99398ca1cd5bd5b84ac3264a4e1b3e65afa1bcee7540c4");
\$v = hash('sha256', hash('sha256', \$tx1.\$tx2, true), true );

echo be_hex(\$v), "\n";
``````

output

``````d47780c084bad3830bcdaf6eace035e4c6cbf646d103795d22104fb105014ba3
``````