What are the equations to convert between bits and difficulty?


understanding bits and difficulty in a block header

ask how to convert between bits and difficulty. Unfortunately, I do not get the answers because I do not know C (I think it's C) and apparently lack CS knowledge.

Could anyone show in mathematical notation or in high-level code (Python?) how to convert for example the 392009692 bits to the difficulty of 3,007,383,866,429.73? as in block


Thanks a lot!

1 Answer 1


This snippet converts the nbits integer as appearing in your blockchain.info link to the difficulty

def nbits(num):
    # Convert integer to hex
    hexstr = format(num, 'x')
    first_byte, last_bytes = hexstr[0:2], hexstr[2:]
    # convert bytes back to int
    first, last = int(first_byte, 16), int(last_bytes, 16)
    return last * 256 ** (first - 3)

def difficulty(num):
    # Difficulty of genesis block / current
    return 0x00ffff0000000000000000000000000000000000000000000000000000 / nbits(num)

>>> difficulty(392009692)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.