API Reference
decode(vint)
Decode a Variable-Size Integer (VINT).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vint |
bytes
|
The bytes representing the VINT. |
required |
Returns:
Type | Description |
---|---|
int
|
The decoded integer. |
Examples:
>>> decode(b'\x82')
2
>>> decode(b'\x10\x00\x00\x02')
2
>>> decode(b'\xff')
127
>>> decode(b'\x01')
Traceback (most recent call last):
...
ValueError: Invalid VINT.
Source code in src/pyvint/core.py
decode_stream(stream)
Decode a Variable-Size Integer (VINT) from a stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stream |
IOBase[bytes]
|
The stream to read the VINT from. |
required |
Returns:
Type | Description |
---|---|
int
|
The decoded integer. |
Examples:
>>> from io import BytesIO
>>> stream = BytesIO(b'\x82')
>>> decode_stream(stream)
2
>>> stream = BytesIO(b'\x40\x02\x00')
>>> decode_stream(stream)
2
>>> stream.read()
b'\x00'
Source code in src/pyvint/core.py
encode(value, octet_length=None)
Encode an integer to a Variable-Size Integer (VINT). You can specify the octet length of the VINT. If you don't specify it, the function will calculate it automatically. This function doesn't support negative integers, which causes a ValueError to be raised. You will get a ValueError if the octet length is less than the calculated one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
int
|
The integer to encode. |
required |
octet_length |
Optional[int]
|
The octet length of the VINT. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
bytes
|
The bytes representing the VINT. |
Examples:
>>> encode(2)
b'\x82'
>>> encode(89)
b'\xd9'
>>> encode(0)
b'\x80'
>>> encode(0, 2)
b'@\x00'
>>> encode(172351395)
b'\x1aE\xdf\xa3'
>>> encode(2, 2)
b'@\x02'
Source code in src/pyvint/core.py
read_vint(stream)
Read a Variable-Size Integer (VINT) from a stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stream |
IOBase[bytes]
|
The stream to read the VINT from. |
required |
Returns:
Type | Description |
---|---|
bytes
|
The VINT bytes. |
Examples:
>>> from io import BytesIO
>>> stream = BytesIO(b'\x82')
>>> read_vint(stream)
b'\x82'
>>> stream = BytesIO(b'\x40\x02\x00')
>>> read_vint(stream)
b'@\x02'
>>> stream.read()
b'\x00'