Search code examples
hashblockchainsha

How to get numeric value of a hash?


On the Proof of work blockchain wiki, one can find that the hash

0000c3af42fc31103f1fdc0151fa747ff87349a4714df7cc52ea464e12dcd4e9 

corresponds to the value 2^239.61238653. My question is, how can one calculate this numeric value of a hash?


Solution

  • First note that the block hash is usually represented as a hexadecimal, little endian value, when you try to convert to decimal. If your system is little-endian:

    To convert to decimal in bash/perl:

    $ hex=0000c3af42fc31103f1fdc0151fa747ff87349a4714df7cc52ea464e12dcd4e9
    $ $ perl -le 'use bignum qw/hex/; print hex("0x".$ARGV[0])' --"$hex"
    532607621168989936896605052113495566299419916018780134558135832581308350315356027254565114944
    

    or, to get base2 log:

    perl -le 'use bignum qw/hex/; print log(hex($ARGV[0]))/log(2)' -- "$hex" 
    239.6123865298365295145460775449928303015
    

    Which represents 2^239.61238653