<p>The number of inputs/outputs is encoded as a "var_int" or "variable length integer". </p>

<p>Start by looking at the first byte. If it's 252 or less, then that's it, that's your number (like you've been using), there are no more bytes to read.</p>

<p>If the first byte is 253, there are 2 more bytes following it, which are a 16 bit little endian integer. 254 means 4 bytes following it (5 in total), which is a 32 bit integer. 255 means 8 bytes follow, i.e. a 64 bit integer.</p>

<p>You can read more here: <a href="https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer</a></p>

CC BY-SA 3.0
2018-03-05T20:17:09.007Z
2018-03-05T20:17:09.007Z
meshcollider
<p>I wrote a bitcoin transaction parser in python and it flips out when transactions have more than 255 inputs or outputs.  Some transactions have their number of inputs/outputs represented by a 1 byte hex string, others are represented by more.  How do I know (and how do I program my parser to know) whether a tx's representation of inputs/outputs is 1 byte or more?</p>

<transactions><raw-transaction><transaction-input>
How are 'excessive' numbers of inputs and outputs handled in bitcoin transactions?
Mine
# How are 'excessive' numbers of inputs and outputs handled in bitcoin transactions?

(Asked by: username_0 on 2018-03-05)

I wrote a bitcoin transaction parser in python and it flips out when transactions have more than 255 inputs or outputs. Some transactions have their number of inputs/outputs represented by a 1 byte hex string, others are represented by more. How do I know (and how do I program my parser to know) whether a tx's representation of inputs/outputs is 1 byte or more?

**Tags:** transactions, raw-transaction, transaction-input


---

## 1 answers

### Answer by username_1 on 2018-03-05. Score: 4 | Accepted answer

The number of inputs/outputs is encoded as a "var\_int" or "variable length integer".

Start by looking at the first byte. If it's 252 or less, then that's it, that's your number (like you've been using), there are no more bytes to read.

If the first byte is 253, there are 2 more bytes following it, which are a 16 bit little endian integer. 254 means 4 bytes following it (5 in total), which is a 32 bit integer. 255 means 8 bytes follow, i.e. a 64 bit integer.

You can read more here: <https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer>


---

