<p>The size is encoded as Compact size.  These can be up to 9 bytes long and encode a size of anything between zero to 18446744073709551615 bytes.</p>

<p>The rules are (according to <code>serialize.h</code>) :</p>

<pre><code>/**
 * Compact Size
 * size &lt;  253        -- 1 byte
 * size &lt;= USHRT_MAX  -- 3 bytes  (253 + 2 bytes)
 * size &lt;= UINT_MAX   -- 5 bytes  (254 + 4 bytes)
 * size &gt;  UINT_MAX   -- 9 bytes  (255 + 8 bytes)
 */
inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
{
    if (nSize &lt; 253)             return sizeof(unsigned char);
    else if (nSize &lt;= std::numeric_limits&lt;unsigned short&gt;::max()) return sizeof(unsigned char) + sizeof(unsigned short);
    else if (nSize &lt;= std::numeric_limits&lt;unsigned int&gt;::max())  return sizeof(unsigned char) + sizeof(unsigned int);
    else                         return sizeof(unsigned char) + sizeof(uint64_t);
}
</code></pre>

<p>Which basically means, for any data of length <code>L</code> in bytes,</p>

<p>If <code>L &lt; 253</code>, the encoding is just the value itself.  Anything between <code>0x00</code> and <code>0xFC</code>.</p>

<p>For larger sizes, we prepend the encoding with one byte <code>0xFD</code>, <code>0xFE</code> or <code>0xFF</code> depending on how many bytes it would take to encode it.
The sizes <code>253</code>, <code>254</code> and <code>255</code> are an exception (easy to see why.  their bytes are used for the encoding itself) and are treated as a two-byte size.
The size bytes themselves are encoded in little endian.</p>

<p>If <code>253 &lt;= L &lt; 65535</code>, the encoding is first the byte <code>0xFD</code>, then the two bytes that make up the actual size.
Your specifc value of <code>256</code> is in this range, and will be encoded as :</p>

<p><code>FD0001</code></p>

<p><code>FD</code> because it's in the second range, and <code>0001</code> is the "reverse" of <code>0100</code>.</p>

<p>The rest of the ranges behave the same, and their ranges are shown in the code snippet.</p>

CC BY-SA 3.0
2017-12-02T21:44:50.913Z
Note that in the specific case of spending P2SH, you never need 254 or 255, as the maximum allowed redeemscript is 520 bytes.
Pieter Wuille
CC BY-SA 3.0
2017-12-02T21:54:45.173Z
The size bytes themselves are encoded in **little** endian.
amaclin
CC BY-SA 3.0
2017-12-02T21:42:53.153Z
2017-12-02T22:07:38.560Z
2017-12-02T22:07:38.560Z
arubi
<p>I am trying to spend an utxo sent to a p2sh address. The scriptsig (including the redeem script) is longer than 256 bytes. When the transaction is serialized there is only one byte to indicate the size of the scriptsig. How should I do? Thanks by advance. </p>

<raw-transaction><p2sh><input-script>
how to spend an utxo when the corresponding scriptsig is longer than 256 bytes?
benjiw
# how to spend an utxo when the corresponding scriptsig is longer than 256 bytes?

(Asked by: username_0 on 2017-12-02)

I am trying to spend an utxo sent to a p2sh address. The scriptsig (including the redeem script) is longer than 256 bytes. When the transaction is serialized there is only one byte to indicate the size of the scriptsig. How should I do? Thanks by advance.

**Tags:** raw-transaction, p2sh, input-script


---

## 1 answers

### Answer by username_1 on 2017-12-02. Score: 3 | Accepted answer

The size is encoded as Compact size.  These can be up to 9 bytes long and encode a size of anything between zero to 18446744073709551615 bytes.
The rules are (according to serialize.h) :
/**
 * Compact Size
 * size <  253        -- 1 byte
 * size <= USHRT_MAX  -- 3 bytes  (253 + 2 bytes)
 * size <= UINT_MAX   -- 5 bytes  (254 + 4 bytes)
 * size >  UINT_MAX   -- 9 bytes  (255 + 8 bytes)
 */
inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
{
    if (nSize < 253)             return sizeof(unsigned char);
    else if (nSize <= std::numeric_limits::max()) return sizeof(unsigned char) + sizeof(unsigned short);
    else if (nSize <= std::numeric_limits::max())  return sizeof(unsigned char) + sizeof(unsigned int);
    else                         return sizeof(unsigned char) + sizeof(uint64_t);
}

Which basically means, for any data of length L in bytes,
If L < 253, the encoding is just the value itself.  Anything between 0x00 and 0xFC.
For larger sizes, we prepend the encoding with one byte 0xFD, 0xFE or 0xFF depending on how many bytes it would take to encode it.
The sizes 253, 254 and 255 are an exception (easy to see why.  their bytes are used for the encoding itself) and are treated as a two-byte size.
The size bytes themselves are encoded in little endian.
If 253 <= L < 65535, the encoding is first the byte 0xFD, then the two bytes that make up the actual size.
Your specifc value of 256 is in this range, and will be encoded as :
FD0001
FD because it's in the second range, and 0001 is the "reverse" of 0100.
The rest of the ranges behave the same, and their ranges are shown in the code snippet.

> **Comments:**
> **username_2**: Note that in the specific case of spending P2SH, you never need 254 or 255, as the maximum allowed redeemscript is 520 bytes.  
> **username_3**: The size bytes themselves are encoded in **little** endian.  

---

