<pre><code>prev_block = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
</code></pre>

<p>This is simply setting a variable called "prev_block" to the hexadecimal text hash of the previous block. It could have been stored as the raw byte hash, but for ease of use it is stored as hexadecimal text.</p>

<pre><code>prev_block.decode('hex')[::-1]
</code></pre>

<p>This converts the hexadecimal text stored in prev_block into it's binary representation as a string/array of bytes.</p>

<p>If you simply want to create a header then this code will accomplish that as long as you provide all the variables (ver, prev_block, mrkl_root, time_, bits, and nonce):</p>

<pre><code>ver = 2
prev_block = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
mrkl_root = "871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a"
time_ = 0x53058b35 # 2014-02-20 04:57:25
bits = 0x19015f53
nonce = 1 # make sure to change this to the correct nonce
header = ( struct.pack("&lt;L", ver) + prev_block.decode('hex')[::-1] +
      mrkl_root.decode('hex')[::-1] + struct.pack("&lt;LLL", time_, bits, nonce))
print header.encode('hex')
</code></pre>

<h3>Update</h3>

<p>The <code>struct.pack("&lt;L", ver)</code> and <code>struct.pack("&lt;LLL", time_, bits, nonce)</code> calls simply use the first argument (a format string) as instructions to convert the remaining arguments to little endian (also called least significant byte or LSB) unsigned long Byte arrays <a href="https://docs.python.org/2/library/struct.html" rel="nofollow">more details on that here</a>. In the latter case when three "L"'s are used that's because there are 3 integer arguments provided which we want to convert into unsigned longs.</p>

<p><code>[::-1]</code> is used to reverse the bytes of an array (this is necessary because the hashes are stored in textual hex in reverse order from how they are stored in binary which is little endian). So if for example <code>prev_block.decode('hex')</code> returned the Byte values 0, 1, 2, 3 then <code>prev_block.decode('hex')[::-1]</code> would return Byte values 3, 2, 1, 0. <a href="https://docs.python.org/release/2.3.5/whatsnew/section-slices.html" rel="nofollow">More about extended slicing here</a>.</p>

CC BY-SA 3.0
2016-06-18T07:07:42.597Z
`@Brandon` Thank you, and can you explain what `"<L"`, `"<LLL"` & `[::-1]` doing in that, all I could find is `"L"` converts the value to `long` but couldn't find `<L` or `<LLL` is doing. Does the `[::-1]` used to prevent the `overflow exceptions` when reading the value?
Pretty_Girl
CC BY-SA 3.0
2016-06-19T18:11:23.893Z
@Pretty_Girl I see you are also trying to convert this code into VB.net. My suggestion is to simply take the code block I provided above and put that into a python program all of it's own. Then you can mess around with the different functions by changing them or remove them to see how those changes affect the output. That's the best way to learn how the code works in my opinion - get your hands dirty.
Brandon
CC BY-SA 3.0
2016-06-17T19:23:40.363Z
2016-06-19T18:06:11.993Z
2016-06-19T18:06:11.993Z
Brandon
<p>Help me to understand this code, spend all day to understand this code, and learned python from scratch, now I understand what operators do, but still some parts are complex to me.</p>

<pre><code>import hashlib, struct

ver = 2
prev_block = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
mrkl_root = "871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a"
time_ = 0x53058b35 # 2014-02-20 04:57:25
bits = 0x19015f53

# https://en.bitcoin.it/wiki/Difficulty
exp = bits &gt;&gt; 24
mant = bits &amp; 0xffffff
target_hexstr = '%064x' % (mant * (1&lt;&lt;(8*(exp - 3))))
target_str = target_hexstr.decode('hex')

nonce = 0
while nonce &lt; 0x100000000:
    header = ( struct.pack("&lt;L", ver) + prev_block.decode('hex')[::-1] +
          mrkl_root.decode('hex')[::-1] + struct.pack("&lt;LLL", time_, bits, nonce))
    hash = hashlib.sha256(hashlib.sha256(header).digest()).digest()
    print nonce, hash[::-1].encode('hex')
    if hash[::-1] &lt; target_str:
        print 'success'
        break
nonce += 1
</code></pre>

<p>In this code does the </p>

<blockquote>
  <p>prev_block
  "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"</p>
</blockquote>

<p>is in SHA256 and then later it's decoded in to hexadecimal by using </p>

<blockquote>
  <p>prev_block.decode('hex')[::-1]</p>
</blockquote>

<p>?</p>

<p>I just want to create a full "header" using the data I already have.</p>

CC BY-SA 3.0
2016-07-16T20:20:45.993Z
Any chance you can mark my answer as accepted, if that was the answer that helped you?
Brandon
<python>
help me to understand this mining code
Pretty_Girl
# help me to understand this mining code

(Asked by: username_0 on 2016-06-17)

Help me to understand this code, spend all day to understand this code, and learned python from scratch, now I understand what operators do, but still some parts are complex to me.

```
import hashlib, struct

ver = 2
prev_block = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
mrkl_root = "871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a"
time_ = 0x53058b35 # 2014-02-20 04:57:25
bits = 0x19015f53

# https://en.bitcoin.it/wiki/Difficulty
exp = bits >> 24
mant = bits & 0xffffff
target_hexstr = '%064x' % (mant * (1<<(8*(exp - 3))))
target_str = target_hexstr.decode('hex')

nonce = 0
while nonce < 0x100000000:
    header = ( struct.pack("<L", ver) + prev_block.decode('hex')[::-1] +
          mrkl_root.decode('hex')[::-1] + struct.pack("<LLL", time_, bits, nonce))
    hash = hashlib.sha256(hashlib.sha256(header).digest()).digest()
    print nonce, hash[::-1].encode('hex')
    if hash[::-1] < target_str:
        print 'success'
        break
nonce += 1

```

In this code does the

> prev\_block
> "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"

is in SHA256 and then later it's decoded in to hexadecimal by using

> prev\_block.decode('hex')[::-1]

?

I just want to create a full "header" using the data I already have.

**Tags:** python

> **Comments:**
> **username_1**: Any chance you can mark my answer as accepted, if that was the answer that helped you?  

---

## 1 answers

### Answer by username_1 on 2016-06-17. Score: 3

```
prev_block = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"

```

This is simply setting a variable called "prev\_block" to the hexadecimal text hash of the previous block. It could have been stored as the raw byte hash, but for ease of use it is stored as hexadecimal text.

```
prev_block.decode('hex')[::-1]

```

This converts the hexadecimal text stored in prev\_block into it's binary representation as a string/array of bytes.

If you simply want to create a header then this code will accomplish that as long as you provide all the variables (ver, prev\_block, mrkl\_root, time\_, bits, and nonce):

```
ver = 2
prev_block = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
mrkl_root = "871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a"
time_ = 0x53058b35 # 2014-02-20 04:57:25
bits = 0x19015f53
nonce = 1 # make sure to change this to the correct nonce
header = ( struct.pack("<L", ver) + prev_block.decode('hex')[::-1] +
      mrkl_root.decode('hex')[::-1] + struct.pack("<LLL", time_, bits, nonce))
print header.encode('hex')

```

### Update

The `struct.pack("<L", ver)` and `struct.pack("<LLL", time_, bits, nonce)` calls simply use the first argument (a format string) as instructions to convert the remaining arguments to little endian (also called least significant byte or LSB) unsigned long Byte arrays [more details on that here](https://docs.python.org/2/library/struct.html). In the latter case when three "L"'s are used that's because there are 3 integer arguments provided which we want to convert into unsigned longs.

`[::-1]` is used to reverse the bytes of an array (this is necessary because the hashes are stored in textual hex in reverse order from how they are stored in binary which is little endian). So if for example `prev_block.decode('hex')` returned the Byte values 0, 1, 2, 3 then `prev_block.decode('hex')[::-1]` would return Byte values 3, 2, 1, 0. [More about extended slicing here](https://docs.python.org/release/2.3.5/whatsnew/section-slices.html).

> **Comments:**
> **username_0**: `@username_1` Thank you, and can you explain what `"<L"`, `"<LLL"` & `[::-1]` doing in that, all I could find is `"L"` converts the value to `long` but couldn't find `<L` or `<LLL` is doing. Does the `[::-1]` used to prevent the `overflow exceptions` when reading the value?  
> **username_1**: @username_0 I see you are also trying to convert this code into VB.net. My suggestion is to simply take the code block I provided above and put that into a python program all of it's own. Then you can mess around with the different functions by changing them or remove them to see how those changes affect the output. That's the best way to learn how the code works in my opinion - get your hands dirty.  

---

