<p>You seem to be interpreting the hex encoded data as a UTF-8 string. Instead, you should be decoding it as a byte dump.</p>

<p>To illustrate, decoding <code>12</code> as a UTF-8 string will give you:</p>

<pre><code>0x3132
</code></pre>

<p>while decoding it as a hex dump will give you:</p>

<pre><code>0x12
</code></pre>

<p>In bitcoin, all hashing it done on the byte values, which are often printed as hex strings for convenience - you must decode them back into a byte array as hex strings, not as actual strings.</p>

CC BY-SA 4.0
2020-02-17T14:53:56.290Z
Thank you, it solved the problem. :)
Pretty Girl
CC BY-SA 4.0
2020-02-17T14:58:30.687Z
I have another dumb question, suppose now I got the hash lower than the target, then what should I do? how to insert it to the block chain that it will generate the reward to my wallet. no need of code, just some information would be great. should I hash again with my wallet address etc and send it to some ip adress etc (web request etc)? I really have no idea.
Pretty Girl
CC BY-SA 4.0
2020-02-17T15:12:52.183Z
You're supposed to be hashing the result of `getblocktemplate`, which will include a coinbase transaction that pays the reward to your wallet. You cannot choose to pay the reward to yourself after the hashing is already done. If you find a valid block,, you can use the `submitblock` RPC call to submit it to the network
Raghav Sood
CC BY-SA 4.0
2020-02-17T15:25:13.777Z
getblocktemplate is to connect to the mining pools right? I will not use mining pools, I will mine as a sole miner (I know it's nearly impossible to find a block) I want to submit the block myself. currently I hash with `version`, `previous block hash`, `merkle root`, `time stamp`, `bits`, `nonce`.  , where do I hash my wallet etc to receive the reward?
Pretty Girl
CC BY-SA 4.0
2020-02-17T15:30:38.987Z
The block template can be used to mine to your own wallet as well. Your own wallet goes in the coinbase transaction, which is part of the merkle tree, which leads to the merkle root in your block header.
Raghav Sood
CC BY-SA 4.0
2020-02-17T16:37:52.260Z
I'm sorry, I still don't get it, the `getblocktemplate` contains  "previousblockhash", 
  ` "transactions": [],
   "expires": 
   "target":  
   "longpollid":  
   "height":  
   "version": 
   "curtime":  
   "mutable":  
   "bits" `  .. I don't think if I'm a sole miner, I won't be needing all those stuff to hash, and further, `getblocktemplate` doesn't have any where to include my wallet address etc. please elaborate a little bit more if you don't mind.
Pretty Girl
CC BY-SA 4.0
2020-02-17T16:45:38.587Z
and bitcoin.org says to create the blockheader only `version, previous block hash, merkle root, time stamp, bits, nonce` is needed, then why are you saying that I should hash according to the `getblocktemplate`...    how can I include my wallet address so when I find a good hash, I will receive the reward
Pretty Girl
CC BY-SA 4.0
2020-02-17T11:29:33.197Z
2020-02-17T11:29:33.197Z
Raghav Sood
<p>I'm trying to build a bitcoin miner. I have generated the message successfully, but I can't get the <code>double SHA256</code> correctly. The value I'm getting is incorrect. What is wrong with this code?</p>

<pre><code> Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click

    Dim message_combined as string = "02000000671D0E2FF45DD1E927A51219D1CA1065C93B0C4E8840290A00000000000000002CD900FC3513260DF5BD2EABFD456CD2B3D2BACE30CC078215A907C045F4992E3E5F4A5E747B1B1843F740C0"


    Dim sha256 As SHA256 = SHA256Managed.Create()
    Dim bytes As Byte() = Encoding.UTF8.GetBytes(message_combined)
    Dim hash As Byte() = sha256.ComputeHash(bytes)
    Dim stringBuilder1 As New StringBuilder()

    For i As Integer = 0 To hash.Length - 1
        stringBuilder1.Append(hash(i).ToString("X2"))
    Next

    '  Dim sha256 As SHA256 = SHA256Managed.Create()
    Dim sha256_2 As SHA256 = SHA256Managed.Create()
    Dim bytes2 As Byte() = Encoding.UTF8.GetBytes(stringBuilder1.ToString())
    Dim hash2 As Byte() = sha256_2.ComputeHash(bytes2)
    'bytes2 = Encoding.UTF8.GetBytes(bytes)
    'hash2 = sha256_2.ComputeHash(bytes2)
    Dim stringBuilder2 As New StringBuilder()

    For i As Integer = 0 To hash2.Length - 1
        stringBuilder2.Append(hash2(i).ToString("X2"))
    Next


    final_hash = stringBuilder2.ToString()

    TextBox9.Text = final_hash
End Sub
</code></pre>

<p>After double hashing, the end hash should be: 920F0D3EF835E723DBDA8AE0E1EA0705FAF53AC784A8116639BCB215F3BC9D8B</p>

<hash>
Double SHA256 giving me an incorrect value with this code VB.net
Pretty Girl
# Double SHA256 giving me an incorrect value with this code VB.net

(Asked by: username_0 on 2020-02-17)

I'm trying to build a bitcoin miner. I have generated the message successfully, but I can't get the `double SHA256` correctly. The value I'm getting is incorrect. What is wrong with this code?

```
 Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click

    Dim message_combined as string = "02000000671D0E2FF45DD1E927A51219D1CA1065C93B0C4E8840290A00000000000000002CD900FC3513260DF5BD2EABFD456CD2B3D2BACE30CC078215A907C045F4992E3E5F4A5E747B1B1843F740C0"

    Dim sha256 As SHA256 = SHA256Managed.Create()
    Dim bytes As Byte() = Encoding.UTF8.GetBytes(message_combined)
    Dim hash As Byte() = sha256.ComputeHash(bytes)
    Dim stringBuilder1 As New StringBuilder()

    For i As Integer = 0 To hash.Length - 1
        stringBuilder1.Append(hash(i).ToString("X2"))
    Next

    '  Dim sha256 As SHA256 = SHA256Managed.Create()
    Dim sha256_2 As SHA256 = SHA256Managed.Create()
    Dim bytes2 As Byte() = Encoding.UTF8.GetBytes(stringBuilder1.ToString())
    Dim hash2 As Byte() = sha256_2.ComputeHash(bytes2)
    'bytes2 = Encoding.UTF8.GetBytes(bytes)
    'hash2 = sha256_2.ComputeHash(bytes2)
    Dim stringBuilder2 As New StringBuilder()

    For i As Integer = 0 To hash2.Length - 1
        stringBuilder2.Append(hash2(i).ToString("X2"))
    Next

    final_hash = stringBuilder2.ToString()

    TextBox9.Text = final_hash
End Sub

```

After double hashing, the end hash should be: 920F0D3EF835E723DBDA8AE0E1EA0705FAF53AC784A8116639BCB215F3BC9D8B

**Tags:** hash


---

## 1 answers

### Answer by username_1 on 2020-02-17. Score: 1 | Accepted answer

You seem to be interpreting the hex encoded data as a UTF-8 string. Instead, you should be decoding it as a byte dump.

To illustrate, decoding `12` as a UTF-8 string will give you:

```
0x3132

```

while decoding it as a hex dump will give you:

```
0x12

```

In bitcoin, all hashing it done on the byte values, which are often printed as hex strings for convenience - you must decode them back into a byte array as hex strings, not as actual strings.

> **Comments:**
> **username_0**: Thank you, it solved the problem. :)  
> **username_0**: I have another dumb question, suppose now I got the hash lower than the target, then what should I do? how to insert it to the block chain that it will generate the reward to my wallet. no need of code, just some information would be great. should I hash again with my wallet address etc and send it to some ip adress etc (web request etc)? I really have no idea.  
> **username_1**: You're supposed to be hashing the result of `getblocktemplate`, which will include a coinbase transaction that pays the reward to your wallet. You cannot choose to pay the reward to yourself after the hashing is already done. If you find a valid block,, you can use the `submitblock` RPC call to submit it to the network  
> **username_0**: getblocktemplate is to connect to the mining pools right? I will not use mining pools, I will mine as a sole miner (I know it's nearly impossible to find a block) I want to submit the block myself. currently I hash with `version`, `previous block hash`, `merkle root`, `time stamp`, `bits`, `nonce`.  , where do I hash my wallet etc to receive the reward?  
> **username_1**: The block template can be used to mine to your own wallet as well. Your own wallet goes in the coinbase transaction, which is part of the merkle tree, which leads to the merkle root in your block header.  
> **username_0**: I'm sorry, I still don't get it, the `getblocktemplate` contains  "previousblockhash",    ` "transactions": [],    "expires":     "target":      "longpollid":      "height":      "version":     "curtime":      "mutable":      "bits" `  .. I don't think if I'm a sole miner, I won't be needing all those stuff to hash, and further, `getblocktemplate` doesn't have any where to include my wallet address etc. please elaborate a little bit more if you don't mind.  
> **username_0**: and bitcoin.org says to create the blockheader only `version, previous block hash, merkle root, time stamp, bits, nonce` is needed, then why are you saying that I should hash according to the `getblocktemplate`...    how can I include my wallet address so when I find a good hash, I will receive the reward  

---

