<p>This number is encoded as a <a href="https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer" rel="nofollow noreferrer">variable-length integer</a>, see the <code>txn_count</code> in the <a href="https://en.bitcoin.it/wiki/Protocol_documentation#block" rel="nofollow noreferrer">block protocol message</a>.</p>
<p>The idea is the following:</p>
<ul>
<li>Read the first byte as <code>marker</code></li>
<li>If <code>marker &lt; 0xFD</code>
<ul>
<li><code>marker</code> is the number</li>
</ul>
</li>
<li>If <code>marker == 0xFD</code>
<ul>
<li>read the next 2 bytes as the number</li>
</ul>
</li>
<li>If <code>marker == 0xFE</code>
<ul>
<li>read the next 4 bytes as the number</li>
</ul>
</li>
<li>If <code>marker == 0xFF</code>
<ul>
<li>read the next 8 bytes as the number</li>
</ul>
</li>
</ul>
<p>Or, in Python:</p>
<pre><code>def read_varint(byte_array):
    marker = byte_array[0]
    if marker &lt; 0xfd:
        return marker
    elif marker == 0xfd:
        return int.from_bytes(byte_array[1:3], 'little')
    elif marker == 0xfe:
        return int.from_bytes(byte_array[1:5], 'little')
    else:
        return int.from_bytes(byte_array[1:9], 'little')
</code></pre>
<p>See also the implementation in Bitcoin Core <a href="https://github.com/bitcoin/bitcoin/blob/5ba5becbb5d8c794efe579caeea7eea64f895a13/src/serialize.h#L350-L450" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 4.0
2021-02-27T12:01:03.033Z
Thanks a lot. BTW, this `varint` algo is not a "watch ahead" I think :)
ntk138
CC BY-SA 4.0
2021-02-27T12:03:42.970Z
Oh i need to edit my answer anyways as i cleaned up your question to make it more concise :) Also, not sure about what you mean by "watch ahead algorithm" ?
Antoine Poinsot
CC BY-SA 4.0
2021-02-27T12:08:43.870Z
"watch ahead" is read some extra data ahead to determine the meaning of the current token. e.g. here if the algo needs to read some bytes of the TX content, then it is a watch ahead. `varint` is a clever algo does not need to watch ahead.
ntk138
CC BY-SA 4.0
2021-02-27T12:16:57.440Z
Ok, thanks!     (random characters here to make the minimum comment length)
Antoine Poinsot
CC BY-SA 4.0
2021-02-27T11:41:52.377Z
2021-02-27T12:04:07.847Z
2021-02-27T12:04:07.847Z
Antoine Poinsot
<p>In the <a href="https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch09.asciidoc#structure-of-a-block" rel="nofollow noreferrer">bitcoin-book</a>, the &quot;Transaction Counter&quot; part of a block is documented as being 1~9 bytes long instead of a fixed size.</p>
<p>How is this counter parsed from the raw data?</p>

<block><raw-transaction>
How to deserialize the Transaction Count in a raw block?
ntk138
# How to deserialize the Transaction Count in a raw block?

(Asked by: username_0 on 2021-02-27)

In the [bitcoin-book](https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch09.asciidoc#structure-of-a-block), the "Transaction Counter" part of a block is documented as being 1~9 bytes long instead of a fixed size.

How is this counter parsed from the raw data?

**Tags:** block, raw-transaction


---

## 1 answers

### Answer by username_1 on 2021-02-27. Score: 1 | Accepted answer

This number is encoded as a variable-length integer, see the txn_count in the block protocol message.
The idea is the following:

Read the first byte as marker
If marker < 0xFD

marker is the number


If marker == 0xFD

read the next 2 bytes as the number


If marker == 0xFE

read the next 4 bytes as the number


If marker == 0xFF

read the next 8 bytes as the number



Or, in Python:
def read_varint(byte_array):
    marker = byte_array[0]
    if marker < 0xfd:
        return marker
    elif marker == 0xfd:
        return int.from_bytes(byte_array[1:3], 'little')
    elif marker == 0xfe:
        return int.from_bytes(byte_array[1:5], 'little')
    else:
        return int.from_bytes(byte_array[1:9], 'little')

See also the implementation in Bitcoin Core here.

> **Comments:**
> **username_0**: Thanks a lot. BTW, this `varint` algo is not a "watch ahead" I think :)  
> **username_1**: Oh i need to edit my answer anyways as i cleaned up your question to make it more concise :) Also, not sure about what you mean by "watch ahead algorithm" ?  
> **username_0**: "watch ahead" is read some extra data ahead to determine the meaning of the current token. e.g. here if the algo needs to read some bytes of the TX content, then it is a watch ahead. `varint` is a clever algo does not need to watch ahead.  
> **username_1**: Ok, thanks!     (random characters here to make the minimum comment length)  

---

