Difference between revisions of "VarInt"

Line 6: Line 6:
  
 
When expressing an integer value less than or equal to <code>0xFC</code> (252) the value itself can be used.
 
When expressing an integer value less than or equal to <code>0xFC</code> (252) the value itself can be used.
===Example===
+
 
 +
When expressing an integer value greater than <code>0xFC</code> but less than or equal to <code>0xFFFF</code> (65,535), the varint is <code>0xFDXXXX</code> where XXXX represents the two byte integer
 +
 
 +
When expressing an integer value greater than <code>0xFFFF</code> but less than or equal to <code>0xFFFFFFFF</code> (4,294,967,295), the varint is <code>0xFEXXXXXXXX</code> where XXXXXXXX represents the 4 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"
 
{| class="wikitable"
Line 14: Line 21:
 
! VarInt
 
! VarInt
 
|-
 
|-
| 187
+
| 187 (<code>0xBB</code>)
 
| 1 byte
 
| 1 byte
 
| <code>0xBB</code>
 
| <code>0xBB</code>
 +
|-
 +
| 255 (<code>0xFF</code>)
 +
| 1 byte but greater than <code>0xFC</code>
 +
| <code>0xFD00FF</code>
 +
|-
 +
| 13,337 (<code>0x3419</code>)
 +
| 2 bytes
 +
| <code>0xFD3419</code>
 +
|-
 +
| 134,250,981 (<code>0x80081e5</code>)
 +
| 4 bytes
 +
| <code>0xFE080081e5</code>
 +
|-
 +
| 198,849,843,832,919 (<code>0xB4DA564E2857</code>)
 +
| 6 bytes
 +
| <code>0xFF00B4DA564E2857</code>
 
|}
 
|}
 
When expressing an integer value greater than <code>0xFC</code> but less than or equal to <code>0xFFFF</code> (65,535), the varint is <code>0xFDXXXX</code> where XXXX represents the two byte integer
 
 
When expressing an integer value greater than <code>0xFFFF</code> but less than or equal to <code>0xFFFFFFFF</code> (4,294,967,295), the varint is <code>0xFEXXXXXXXX</code> where XXXXXXXX represents the 4 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
 

Revision as of 01:27, 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.

Using VarInts

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

Value Size VarInt
187 (0xBB) 1 byte 0xBB
255 (0xFF) 1 byte but greater than 0xFC 0xFD00FF
13,337 (0x3419) 2 bytes 0xFD3419
134,250,981 (0x80081e5) 4 bytes 0xFE080081e5
198,849,843,832,919 (0xB4DA564E2857) 6 bytes 0xFF00B4DA564E2857