1

Questions

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

https://blockchain.info/block/00000000000000000025c089d0a7b2bf6241888c4dd90ab7a4c4baa6a2823551

Thanks a lot!

1 Answers1

0

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)
3007383866429.732
Mike D
  • 3,569
  • 1
  • 9
  • 19