<p>I have written this code to create and submit a new minned block to the bitcoin network:</p>

<pre><code>  unsigned long magic_number = 0xD9B4BEF9;
  unsigned long block_size = 89 + (strlen(coinbase_transaction)-1) + (length*(strlen(root[0])-1));
  unsigned char transaction_counter = length;

  char* block = malloc(block_size);
  memcpy(block, &amp;magic_number, 4); block += 4;
  memcpy(block, &amp;block_size, 4); block += 4;
  memcpy(block, &amp;header, 80); block += 80;
  memcpy(block, &amp;transaction_counter, 1); block += 1;
  memcpy(block, &amp;coinbase_transaction, strlen(coinbase_transaction)-1); block += strlen(coinbase_transaction)-1;
  for(int x=0; x&lt;length; x++) { memcpy(block, &amp;root[x], strlen(root[x])-1); block += strlen(root[x])-1; }

  char* content1 = curl_submitblock(block);
  int size1 = strlen(content1);
  json_char* json1 = (json_char*) content1;
  json_value* value1 = json_parse(json1,size1);
  printf("%s\n", content);
</code></pre>

<p>but, according to the documentation, I need submit a hex string of this block. For what I understand, the first 89 characters of the string <code>block</code> in this code is not a hex string.</p>

<p>What the way to make a hex string of all the block content, given the code above (if it's correct)?</p>

CC BY-SA 3.0
2017-08-27T12:43:35.473Z
your memcpy calls rewrite the same destination memory
amaclin
CC BY-SA 3.0
2017-08-27T13:12:22.580Z
@amaclin fixed this. now if i just figure out the other issue.
Kleber Mota
CC BY-SA 3.0
2017-08-27T13:22:27.347Z
you can not use `strlen` call for binary data like `coinbase_transaction`
amaclin
CC BY-SA 3.0
2017-08-27T13:30:15.947Z
but `coinbase_transaction` is not binary (at least in this code), but `unsigned char*`. Is this wrong?
Kleber Mota
<transactions><block><mining-theory><coinbase-transaction><block-header>
How generate a hex string for a block
Kleber Mota
# How generate a hex string for a block

(Asked by: username_0 on 2017-08-27)

I have written this code to create and submit a new minned block to the bitcoin network:

```
  unsigned long magic_number = 0xD9B4BEF9;
  unsigned long block_size = 89 + (strlen(coinbase_transaction)-1) + (length*(strlen(root[0])-1));
  unsigned char transaction_counter = length;

  char* block = malloc(block_size);
  memcpy(block, &magic_number, 4); block += 4;
  memcpy(block, &block_size, 4); block += 4;
  memcpy(block, &header, 80); block += 80;
  memcpy(block, &transaction_counter, 1); block += 1;
  memcpy(block, &coinbase_transaction, strlen(coinbase_transaction)-1); block += strlen(coinbase_transaction)-1;
  for(int x=0; x<length; x++) { memcpy(block, &root[x], strlen(root[x])-1); block += strlen(root[x])-1; }

  char* content1 = curl_submitblock(block);
  int size1 = strlen(content1);
  json_char* json1 = (json_char*) content1;
  json_value* value1 = json_parse(json1,size1);
  printf("%s\n", content);

```

but, according to the documentation, I need submit a hex string of this block. For what I understand, the first 89 characters of the string `block` in this code is not a hex string.

What the way to make a hex string of all the block content, given the code above (if it's correct)?

**Tags:** transactions, block, mining-theory, coinbase-transaction, block-header

> **Comments:**
> **username_1**: your memcpy calls rewrite the same destination memory  
> **username_0**: @username_1 fixed this. now if i just figure out the other issue.  
> **username_1**: you can not use `strlen` call for binary data like `coinbase_transaction`  
> **username_0**: but `coinbase_transaction` is not binary (at least in this code), but `unsigned char*`. Is this wrong?  

---

## 0 answers

