Difference between revisions of "VarInt"
Line 27: | Line 27: | ||
| 253 <= value <= 65,535 (<code>0xFFFF</code>) <br> <br> 255 (<code>0xFF</code>) <br> <br> 13,337 (<code>0x3419</code>) | | 253 <= value <= 65,535 (<code>0xFFFF</code>) <br> <br> 255 (<code>0xFF</code>) <br> <br> 13,337 (<code>0x3419</code>) | ||
| <br> <br> 1 byte but greater than <code>0xFC</code> <br> <br> 2 bytes | | <br> <br> 1 byte but greater than <code>0xFC</code> <br> <br> 2 bytes | ||
− | | 0xFD + number as uint16_t | + | | 0xFD + number as uint16_t <br> <br> <code>0xFD00FF</code> <br> <br> <code>0xFD3419</code> |
|- | |- | ||
| 14,435,729 (<code>0xDC4591</code>) | | 14,435,729 (<code>0xDC4591</code>) |
Revision as of 04:34, 15 June 2020
A VarInt or "Variable Integer" is an integer format used widely in Bitcoin to indicate the lengths of fields within transaction, block and peer-to-peer network 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 between zero and 0xFC
(252) the value itself can be used expressed in uint8_t format.
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 in uint16_t format.
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 in uint32_t format.
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 in uint64_t format.
References with Examples
Value example value |
Size | VarInt format example values |
---|---|---|
0 <= value <= 252 (0xFC ) e.g. 187 ( 0xBB )
|
1 byte |
uint8_t 0xBB
|
253 <= value <= 65,535 (0xFFFF ) 255 ( 0xFF ) 13,337 ( 0x3419 )
|
1 byte but greater than 0xFC 2 bytes |
0xFD + number as uint16_t 0xFD00FF 0xFD3419
|
14,435,729 (0xDC4591 )
|
3 bytes | 0xFE00DC4591
|
134,250,981 (0x80081E5 )
|
4 bytes | 0xFE080081E5
|
198,849,843,832,919 (0xB4DA564E2857 )
|
6 bytes | 0xFF0000B4DA564E2857
|
5,473,425,651,754,713,432 (0x4BF583A17D59C158 )
|
8 bytes | 0xFF4BF583A17D59C158
|