<p>From the <a href="https://bitcointalk.org/index.php?topic=32849.msg410480#msg410480" rel="noreferrer">Bitcoin forum</a>:</p>

<blockquote>
  <p>The way the variable length integer works is:</p>
  
  <ul>
  <li>Look at the first byte</li>
  <li>If that first byte is less than 253, use the byte literally</li>
  <li>If that first byte is 253, read the next two bytes as a little endian 16-bit number (total bytes read = 3)</li>
  <li>If that first byte is 254, read the next four bytes as a little endian 32-bit number (total bytes read = 5)</li>
  <li>If that first byte is 255, read the next eight bytes as a little endian 64-bit number (total bytes read = 9)</li>
  </ul>
</blockquote>

CC BY-SA 3.0
2017-08-20T22:49:43.570Z
2017-08-20T22:49:43.570Z
Sam Jones
<blockquote>
  <p>How would you put a count of 256 inputs in to this field?</p>
</blockquote>

<p>FD 00 01 </p>

<p>(00 01) is low-endian representation of decimal(256)</p>

CC BY-SA 3.0
2015-09-09T09:17:13.593Z
Thanks. So all values above 0xFD are little-endian? Also, would decimal(65536) be FE 00 00 01?
inersha
CC BY-SA 3.0
2015-09-11T19:24:43.110Z
no. (FE 00 00 01 00) because FE is prefix for 32-bit low-endian
amaclin
CC BY-SA 3.0
2015-09-08T20:57:23.240Z
2015-09-08T20:57:23.240Z
amaclin
<p>I'm trying to write a script that constructs a serialized bitcoin transaction from scratch.</p>

<p>But when it comes to the <strong>number of inputs</strong>, the field is a <em>variable length integer</em>.</p>

<p>It's typically only 1 byte (2 characters) in length and you do not have to do anything with it, because bitcoin transactions rarely have more than 0xFE (253) inputs.</p>

<p><strong>But what happens when there are more than 253 inputs and the size of the field has to increase to accommodate it?</strong></p>

<p><strong>How would you put a count of 256 inputs in to this field?</strong></p>

<p><a href="https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer" rel="nofollow">https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer</a></p>

<transactions><raw-transaction>
How does the variable length integer work?
inersha
# How does the variable length integer work?

(Asked by: username_0 on 2015-09-08)

I'm trying to write a script that constructs a serialized bitcoin transaction from scratch.

But when it comes to the **number of inputs**, the field is a *variable length integer*.

It's typically only 1 byte (2 characters) in length and you do not have to do anything with it, because bitcoin transactions rarely have more than 0xFE (253) inputs.

**But what happens when there are more than 253 inputs and the size of the field has to increase to accommodate it?**

**How would you put a count of 256 inputs in to this field?**

<https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer>

**Tags:** transactions, raw-transaction


---

## 2 answers

### Answer by username_1 on 2017-08-20. Score: 11

From the [Bitcoin forum](https://bitcointalk.org/index.php?topic=32849.msg410480#msg410480):

> The way the variable length integer works is:
>
> * Look at the first byte
> * If that first byte is less than 253, use the byte literally
> * If that first byte is 253, read the next two bytes as a little endian 16-bit number (total bytes read = 3)
> * If that first byte is 254, read the next four bytes as a little endian 32-bit number (total bytes read = 5)
> * If that first byte is 255, read the next eight bytes as a little endian 64-bit number (total bytes read = 9)


---

### Answer by username_2 on 2015-09-08. Score: 2

> How would you put a count of 256 inputs in to this field?

FD 00 01

(00 01) is low-endian representation of decimal(256)

> **Comments:**
> **username_0**: Thanks. So all values above 0xFD are little-endian? Also, would decimal(65536) be FE 00 00 01?  
> **username_2**: no. (FE 00 00 01 00) because FE is prefix for 32-bit low-endian  

---

