How generate a hex string for a block


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)?

Kleber Mota

Posted 2017-08-27T12:41:02.940

Reputation: 131

your memcpy calls rewrite the same destination memory – amaclin – 2017-08-27T12:43:35.473

@amaclin fixed this. now if i just figure out the other issue. – Kleber Mota – 2017-08-27T13:12:22.580

you can not use strlen call for binary data like coinbase_transaction – amaclin – 2017-08-27T13:22:27.347

but coinbase_transaction is not binary (at least in this code), but unsigned char*. Is this wrong? – Kleber Mota – 2017-08-27T13:30:15.947

No answers