<p>Since <code>pblock</code> is a pointer, you are printing an address. Dereference it to get the <a href="https://github.com/bitcoin/bitcoin/blob/a0988140b71485ad12c3c3a4a9573f7c21b1eff8/src/primitives/block.cpp#L16" rel="nofollow noreferrer">format</a> you want:</p>
<pre><code>std::cout &lt;&lt; *pblock &lt;&lt; std::endl;
</code></pre>

CC BY-SA 4.0
2022-07-26T09:13:30.427Z
2022-07-26T13:05:08.020Z
2022-07-26T13:05:08.020Z
glozow
<p>I have tried to serialize as follows without success: bitcoin tag v22.0 from github.</p>
<p>In the file <code>src/miner.cpp</code> and the function</p>
<p><a href="https://github.com/bitcoin/bitcoin/blob/v22.0/src/miner.cpp#L102" rel="nofollow noreferrer"><code>function std::unique_ptr&lt;CBlockTemplate&gt; BlockAssembler::CreateNewBlock(const CScript&amp; scriptPubKeyIn)</code></a></p>
<p>I have placed the line <code>std::cout &lt;&lt; pblock &lt;&lt; std::endl;</code> at the very end before it returns (<a href="https://github.com/bitcoin/bitcoin/blob/v22.0/src/miner.cpp#L185" rel="nofollow noreferrer">Here</a>). The value that is printed to the terminal is:</p>
<p><code>0x7fc9f4009410</code></p>
<p>This is too little data, I would expect the entire block data to be serialized and printed in hex. What am I doing wrong? I know pblock is a pointer to a CBlock class, but it should still work.</p>
<p>What is the proper way to serialize CBlock data, and also the CBlockHeader data?</p>
<p>Thank you!</p>

CC BY-SA 4.0
2022-01-27T03:21:03.680Z
Since it is a `std::unique_ptr`, the `<<` operator outputs the pointer value rather than the object pointed to. Try dereferencing the pointer first.
Ava Chow
<bitcoin-core><blockchain><bitcoind>
How to serialize the CBlock Class in C++ and print the hex to terminal?
BlockChain
# How to serialize the CBlock Class in C++ and print the hex to terminal?

(Asked by: username_0 on 2022-01-25)

I have tried to serialize as follows without success: bitcoin tag v22.0 from github.

In the file `src/miner.cpp` and the function

[`function std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)`](https://github.com/bitcoin/bitcoin/blob/v22.0/src/miner.cpp#L102)

I have placed the line `std::cout << pblock << std::endl;` at the very end before it returns ([Here](https://github.com/bitcoin/bitcoin/blob/v22.0/src/miner.cpp#L185)). The value that is printed to the terminal is:

`0x7fc9f4009410`

This is too little data, I would expect the entire block data to be serialized and printed in hex. What am I doing wrong? I know pblock is a pointer to a CBlock class, but it should still work.

What is the proper way to serialize CBlock data, and also the CBlockHeader data?

Thank you!

**Tags:** bitcoin-core, blockchain, bitcoind

> **Comments:**
> **username_1**: Since it is a `std::unique_ptr`, the `<<` operator outputs the pointer value rather than the object pointed to. Try dereferencing the pointer first.  

---

## 1 answers

### Answer by username_2 on 2022-07-26. Score: 2

Since `pblock` is a pointer, you are printing an address. Dereference it to get the [format](https://github.com/bitcoin/bitcoin/blob/a0988140b71485ad12c3c3a4a9573f7c21b1eff8/src/primitives/block.cpp#L16) you want:

```
std::cout << *pblock << std::endl;

```


---

