Difference between revisions of "VarInt"

(Created page with "A VarInt or "Variable Integer" is an integer format used widely in Bitcoin to indicate the lengths of fields within transaction and block data. A VarInt is a variable length...")
 
Line 12: Line 12:
  
 
When expressing an integer value greater than <code>0xFFFFFFFF</code> but less than or equal to <code>0xFFFFFFFFFFFFFFFF</code> (18,446,744,073,709,551,615), the varint is <code>0xFFXXXXXXXXXXXXXXXX</code> where XXXXXXXXXXXXXXXX represents the 8 byte integer
 
When expressing an integer value greater than <code>0xFFFFFFFF</code> but less than or equal to <code>0xFFFFFFFFFFFFFFFF</code> (18,446,744,073,709,551,615), the varint is <code>0xFFXXXXXXXXXXXXXXXX</code> where XXXXXXXXXXXXXXXX represents the 8 byte integer
 +
 +
==Examples==
 +
 +
{| class="wikitable"
 +
|-
 +
! Size
 +
! Description
 +
! Size
 +
|-
 +
|Version no
 +
|currently 2
 +
|4 bytes
 +
|-
 +
|In-counter
 +
| positive integer VI = [[VarInt]]
 +
| 1 - 9 bytes
 +
|-

Revision as of 01:15, 15 June 2020

A VarInt or "Variable Integer" is an integer format used widely in Bitcoin to indicate the lengths of fields within transaction and block data.

A VarInt is a variable length field 1, 3, 5 or 9 bytes in length dependent on the size of the object being defined. The VarInt format is used as it is space efficient over simply using an 8-byte field where variable length objects are used.

The VarInt works as follows:

When expressing an integer value less than or equal to 0xFC (252) the value itself can be used.

When expressing an integer value greater than 0xFC but less than or equal to 0xFFFF (65,535), the varint is 0xFDXXXX where XXXX represents the two byte integer

When expressing an integer value greater than 0xFFFF but less than or equal to 0xFFFFFFFF (4,294,967,295), the varint is 0xFEXXXXXXXX where XXXXXXXX represents the 4 byte integer

When expressing an integer value greater than 0xFFFFFFFF but less than or equal to 0xFFFFFFFFFFFFFFFF (18,446,744,073,709,551,615), the varint is 0xFFXXXXXXXXXXXXXXXX where XXXXXXXXXXXXXXXX represents the 8 byte integer

Examples

Size Description Size
Version no currently 2 4 bytes
In-counter positive integer VI = VarInt 1 - 9 bytes