Skip to content

Class IntExtensions

Namespace: Void.Minecraft.Buffers.Extensions
Assembly: Void.Minecraft.dll

public static class IntExtensions

Inheritance

objectIntExtensions

Inherited Members

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Methods

AsVarInt(int)

Encodes value into a new byte array using the Minecraft VarInt format.

public static byte[] AsVarInt(this int value)

Parameters

value int

The 32-bit signed value to encode.

Returns

byte[]

A new array containing only the encoded VarInt bytes (length from 1 to 5).

Examples

byte[] payload = 300.AsVarInt();
// payload can be used as a compact packet field representation.

Remarks

This method allocates a new array for every call and copies the encoded payload from an internal stack buffer.

Negative values are encoded from their two's-complement bit pattern and therefore produce 5 bytes.

See Also

IntExtensions.VarIntSize(int)

AsVarInt(int, Span<byte>)

Encodes value as a Minecraft VarInt into buffer.

public static int AsVarInt(this int value, Span<byte> buffer)

Parameters

value int

The 32-bit signed value to encode.

buffer Span<byte>

The destination span that receives encoded bytes starting at index 0. The span must be large enough for the encoded value.

Returns

int

The number of bytes written to buffer (from 1 to 5).

Examples

Span<byte> bytes = stackalloc byte[5];
var length = 300.AsVarInt(bytes);
// bytes[..length] now contains the VarInt payload.

Remarks

Encoding uses the standard 7-bit continuation format used by Minecraft packets. Negative values are encoded from their two's-complement bit pattern and therefore use 5 bytes.

The method writes only the returned byte count and does not clear any remaining bytes in buffer.

Exceptions

IndexOutOfRangeException

Thrown when buffer is too small for the encoded value.

See Also

IntExtensions.VarIntSize(int)

EnumerateVarInt(int)

Lazily enumerates bytes of value encoded as a Minecraft VarInt.

[Obsolete("Use AsVarInt instead.")]
public static IEnumerable<byte> EnumerateVarInt(int value)

Parameters

value int

The 32-bit signed integer to encode.

Returns

IEnumerable<byte>

An that yields encoded bytes in wire order, from first to last byte.

Examples

foreach (var b in IntExtensions.EnumerateVarInt(300))
{
    // consume each encoded VarInt byte
}

Remarks

This API is obsolete; prefer or for clearer ownership and lower overhead.

Enumeration executes the encoding loop on demand each time the sequence is iterated. The sequence length is from 1 to 5 bytes, and negative values produce 5 bytes.

See Also

IntExtensions.VarIntSize(int), IntExtensions.AsVarInt(int)

VarIntSize(int)

Computes the number of bytes required to encode value as a Minecraft VarInt.

public static int VarIntSize(this int value)

Parameters

value int

The 32-bit signed integer to evaluate.

Returns

int

The encoded VarInt length in bytes, always between 1 and 5.

Examples

var idLength = packetId.VarIntSize();
binaryMessage.Stream.Position = idLength;

Remarks

This method performs a branch-free bit operation and does not allocate.

The result matches the byte count written by for the same value, which allows callers to precompute packet offsets and payload lengths.

Because Minecraft VarInt uses the two's-complement bit pattern of signed integers, negative values always require 5 bytes.

See Also

IntExtensions.AsVarInt(int), IntExtensions.AsVarInt(int, Span<byte>)