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

<pre><code>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)

&gt;&gt;&gt; difficulty(392009692)
3007383866429.732
</code></pre>

CC BY-SA 4.0
2018-05-09T08:58:26.470Z
2018-05-09T08:58:26.470Z
Mike D
<p>Questions </p>

<p><a href="https://bitcoin.stackexchange.com/questions/30467/what-are-the-equations-to-convert-between-bits-and-difficulty/30470?newreg=7c619da0b06f42338bb2ef3448a869e2">What are the equations to convert between bits and difficulty?</a></p>

<p>&amp;</p>

<p><a href="https://bitcoin.stackexchange.com/questions/71578/understanding-bits-and-difficulty-in-a-block-header">understanding bits and difficulty in a block header</a></p>

<p>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.</p>

<p>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</p>

<p><a href="https://blockchain.info/block/00000000000000000025c089d0a7b2bf6241888c4dd90ab7a4c4baa6a2823551" rel="nofollow noreferrer">https://blockchain.info/block/00000000000000000025c089d0a7b2bf6241888c4dd90ab7a4c4baa6a2823551</a></p>

<p>Thanks a lot!</p>

<blockchain><block><difficulty>
In Math or Python notation - How to convert bits to difficulty?
Simon Schmickler
# In Math or Python notation - How to convert bits to difficulty?

(Asked by: username_0 on 2018-05-03)

Questions

[What are the equations to convert between bits and difficulty?](https://bitcoin.stackexchange.com/questions/30467/what-are-the-equations-to-convert-between-bits-and-difficulty/30470?newreg=7c619da0b06f42338bb2ef3448a869e2)

&

[understanding bits and difficulty in a block header](https://bitcoin.stackexchange.com/questions/71578/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!

**Tags:** blockchain, block, difficulty


---

## 1 answers

### Answer by username_1 on 2018-05-09. Score: 0 | Accepted answer

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


---

