MeasureVarUInt

Variable-Length Integer Encoding

Windows
MacOS
Linux

References

Module

Core

Header

/Engine/Source/Runtime/Core/Public/Serialization/VarInt.h

Include

#include "Serialization/VarInt.h"

Syntax

uint32 MeasureVarUInt
(
    const void * InData
)

Remarks

Variable-Length Integer Encoding

ZigZag encoding is used to convert signed integers into unsigned integers in a way that allows integers with a small magnitude to have a smaller encoded representation.

An unsigned integer is encoded into 1-9 bytes based on its magnitude. The first byte indicates how many additional bytes are used by the number of leading 1-bits that it has. The additional bytes are stored in big endian order, and the most significant bits of the value are stored in the remaining bits in the first byte. The encoding of the first byte allows the reader to skip over the encoded integer without consuming its bytes individually.

Encoded unsigned integers sort the same in a byte-wise comparison as when their decoded values are compared. The same property does not hold for signed integers due to ZigZag encoding.

32-bit inputs encode to 1-5 bytes. 64-bit inputs encode to 1-9 bytes.

0x0000'0000'0000'0000 - 0x0000'0000'0000'007f : 0b0 1 byte 0x0000'0000'0000'0080 - 0x0000'0000'0000'3fff : 0b10 2 bytes 0x0000'0000'0000'4000 - 0x0000'0000'001f'ffff : 0b110 3 bytes 0x0000'0000'0020'0000 - 0x0000'0000'0fff'ffff : 0b1110 4 bytes 0x0000'0000'1000'0000 - 0x0000'0007'ffff'ffff : 0b11110 5 bytes 0x0000'0008'0000'0000 - 0x0000'03ff'ffff'ffff : 0b111110 6 bytes 0x0000'0400'0000'0000 - 0x0001'ffff'ffff'ffff : 0b1111110_ 7 bytes 0x0002'0000'0000'0000 - 0x00ff'ffff'ffff'ffff : 0b11111110 8 bytes 0x0100'0000'0000'0000 - 0xffff'ffff'ffff'ffff : 0b11111111 9 bytes

Encoding Examples -42 => ZigZag => 0x53 => 0x53 42 => ZigZag => 0x54 => 0x54 0x1 => 0x01 0x12 => 0x12 0x123 => 0x81 0x23 0x1234 => 0x92 0x34 0x12345 => 0xc1 0x23 0x45 0x123456 => 0xd2 0x34 0x56 0x1234567 => 0xe1 0x23 0x45 0x67 0x12345678 => 0xf0 0x12 0x34 0x56 0x78 0x123456789 => 0xf1 0x23 0x45 0x67 0x89 0x123456789a => 0xf8 0x12 0x34 0x56 0x78 0x9a 0x123456789ab => 0xfb 0x23 0x45 0x67 0x89 0xab 0x123456789abc => 0xfc 0x12 0x34 0x56 0x78 0x9a 0xbc 0x123456789abcd => 0xfd 0x23 0x45 0x67 0x89 0xab 0xcd 0x123456789abcde => 0xfe 0x12 0x34 0x56 0x78 0x9a 0xbc 0xde 0x123456789abcdef => 0xff 0x01 0x23 0x45 0x67 0x89 0xab 0xcd 0xef 0x123456789abcdef0 => 0xff 0x12 0x34 0x56 0x78 0x9a 0xbc 0xde 0xf0 Measure the length in bytes (1-9) of an encoded variable-length integer.

Returns

The number of bytes used to encode the integer, in the range 1-9.

Parameters

Parameter

Description

InData

A variable-length encoding of an (signed or unsigned) integer.

Help shape the future of Unreal Engine documentation! Tell us how we're doing so we can serve you better.
Take our survey
Dismiss