UnsafeHelpers

Namespace: SnowBank.Buffers.Binary · class

Helper methods for dealing with unmanaged memory. HANDLE WITH CARE!

Remarks

Use of this class is unsafe. YOU HAVE BEEN WARNED!

Methods

ByteSwap128

static UInt128 ByteSwap128(UInt128 value)

Swap the order of the bytes in a 64-bit word

  • value — 0x0123456789ABCDEF

Returns: 0xEFCDAB8967452301

static Int128 ByteSwap128(Int128 value)

Swap the order of the bytes in a 64-bit word

  • value — 0x0123456789ABCDEF

Returns: 0xEFCDAB8967452301

ByteSwap16

static ushort ByteSwap16(ushort value)

Swap the order of the bytes in a 16-bit word

  • value — 0x0123

Returns: 0x2301

static short ByteSwap16(short value)

Swap the order of the bytes in a 16-bit word

  • value — 0x0123

Returns: 0x2301

ByteSwap24

static uint ByteSwap24(uint value)

Swap the order of the bytes in a 24-bit word

  • value — 0x012345

Returns: 0x452301

Bits 24-31 are ignored

static int ByteSwap24(int value)

Swap the order of the bytes in a 24-bit word

  • value — 0x0123

Returns: 0x2301

Bits 24-31 are ignored

ByteSwap32

static uint ByteSwap32(uint value)

Swap the order of the bytes in a 32-bit word

  • value — 0x01234567

Returns: 0x67452301

static int ByteSwap32(int value)

Swap the order of the bytes in a 32-bit word

  • value — 0x01234567

Returns: 0x67452301

ByteSwap64

static ulong ByteSwap64(ulong value)

Swap the order of the bytes in a 64-bit word

  • value — 0x0123456789ABCDEF

Returns: 0xEFCDAB8967452301

static long ByteSwap64(long value)

Swap the order of the bytes in a 64-bit word

  • value — 0x0123456789ABCDEF

Returns: 0xEFCDAB8967452301

ClearUnsafe

static void ClearUnsafe(Byte* bytes, uint count)

Fill the content of an unmanaged array with zeroes, without checking the arguments

ComputeHashCode

static int ComputeHashCode(ReadOnlySpan<byte> bytes)

Compute the hash code of a byte buffer

This should only be used for dictionaries or hashset that reside in memory only! The hashcode could change at any time in future versions.

static int ComputeHashCode(Byte* bytes, uint count)

Computes the hashcode of a byte buffer

ConvertToByteString

static string ConvertToByteString(ReadOnlySpan<byte> buffer)

Convert a byte stream into a .NET string by expanding each byte to 16 bits characters

Returns: Equivalent .NET string

This is safe to use with 7-bit ASCII strings. You should NOT use this if the buffer contains ANSI or UTF-8 encoded strings! If the buffer contains bytes that are >= 0x80, they will be mapped to the equivalent Unicode code points (0x80..0xFF), WITHOUT converting them using current ANSI code page.

ConvertToByteString(new byte[] { 'A', 'B', 'C' }, 0, 3) => "ABC" ConvertToByteString(new byte[] { 255, 'A', 'B', 'C' }, 0, 4) => "\xffABC" ConvertToByteString(UTF8("é"), ...) => "é" (len=2, 'C3 A9')

static string ConvertToByteString(Byte* pBytes, uint count)

Convert a byte stream into a .NET string by expanding each byte to 16 bits characters

Returns: Equivalent .NET string

This is safe to use with 7-bit ASCII strings. You should NOT use this if the buffer contains ANSI or UTF-8 encoded strings! If the buffer contains bytes that are >= 0x80, they will be mapped to the equivalent Unicode code points (0x80..0xFF), WITHOUT converting them using current ANSI code page.

EnsureCapacity

static byte[] EnsureCapacity(ref byte[] buffer, int minCapacity)

Ensure that the specified temporary buffer is large enough

  • buffer — Pointer to a temporary scratch buffer (previous data will not be maintained)
  • minCapacity — Minimum expected capacity

Returns: Same buffer if it was large enough, or a new allocated buffer with length greater than or equal to minCapacity

FillUnsafe

static void FillUnsafe(Byte* bytes, int count, byte filler)

Fill the content of a managed segment with the same byte repeated

static void FillUnsafe(Byte* bytes, uint count, byte filler)

Fill the content of a managed segment with the same byte repeated

IsAsciiBytes

static bool IsAsciiBytes(ReadOnlySpan<byte> buffer)

Check if a memory region only contains bytes between 0 and 127 (7-bit ASCII)

Returns: False if at least one byte has bit 7 set to 1; otherwise, True.

IsAsciiString

static bool IsAsciiString(ReadOnlySpan<char> value)

Check if a section of a string only contains characters between 0 and 127 (ASCII)

LoadInt16BE

static short LoadInt16BE(Void* ptr)

Load a 16-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 2-byte location

Returns: Logical value in host order

[ 0x12, 0x34 ] == 0x1234

LoadInt16LE

static short LoadInt16LE(Void* ptr)

Load a 16-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 2-byte location

Returns: Logical value in host order

[ 0x34, 0x12 ] == 0x1234

LoadInt24BE

static int LoadInt24BE(Void* ptr)

Load a 24-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 3-byte location

Returns: Logical value in host order

([ 0x12, 0x34, 0x56 ]) => 0x123456

LoadInt24LE

static int LoadInt24LE(Void* ptr)

Load a 24-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 2-byte location

Returns: Logical value in host order

([ 0x56, 0x34, 0x12 ]) => 0x123456

LoadInt32BE

static int LoadInt32BE(Void* ptr)

Load a 32-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 4-byte location

Returns: Logical value in host order

([ 0x12, 0x34, 0x56, 0x78) => 0x12345678

LoadInt32LE

static int LoadInt32LE(Void* ptr)

Load a 32-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 4-byte location

Returns: Logical value in host order

([ 0x78, 0x56, 0x34, 0x12) => 0x12345678

LoadInt64BE

static long LoadInt64BE(Void* ptr)

Load a 64-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of an 8-byte location

Returns: Logical value in host order

([ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF) => 0x0123456789ABCDEF

LoadInt64LE

static long LoadInt64LE(Void* ptr)

Load a 64-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of an 8-byte location

Returns: Logical value in host order

[ 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x456, 0x23, 0x01) => 0x0123456789ABCDEF

LoadUInt16BE

static ushort LoadUInt16BE(Void* ptr)

Load a 16-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 2-byte location

Returns: Logical value in host order

[ 0x12, 0x34 ] == 0x1234

LoadUInt16LE

static ushort LoadUInt16LE(Void* ptr)

Load a 16-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 2-byte location

Returns: Logical value in host order

[ 0x34, 0x12 ] == 0x1234

LoadUInt24BE

static uint LoadUInt24BE(Void* ptr)

Load a 24-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 3-byte location

Returns: Logical value in host order

([ 0x12, 0x34, 0x56 ]) => 0x123456

LoadUInt24LE

static uint LoadUInt24LE(Void* ptr)

Load a 24-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 2-byte location

Returns: Logical value in host order

([ 0x56, 0x34, 0x12 ]) => 0x123456

LoadUInt32BE

static uint LoadUInt32BE(Void* ptr)

Load a 32-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 4-byte location

Returns: Logical value in host order

([ 0x12, 0x34, 0x56, 0x78) => 0x12345678

LoadUInt32LE

static uint LoadUInt32LE(Void* ptr)

Load a 32-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 4-byte location

Returns: Logical value in host order

([ 0x78, 0x56, 0x34, 0x12) => 0x12345678

LoadUInt64BE

static ulong LoadUInt64BE(Void* ptr)

Load a 64-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of an 8-byte location

Returns: Logical value in host order

([ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF) => 0x0123456789ABCDEF

LoadUInt64LE

static ulong LoadUInt64LE(Void* ptr)

Load a 64-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of an 8-byte location

Returns: Logical value in host order

[ 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x456, 0x23, 0x01) => 0x0123456789ABCDEF

Nibble

static int Nibble(char c)

Convert a char in range '0-9A-Fa-f' into a value between 0 and 15

Result is unspecified if char is not in the valid range!

static char Nibble(int x)

Convert values between 0 and 15 into a character from in range '0-9A-F'

Only the lower 4 bits are used, so the caller does not need to mask out the upper bits!

ReadInt16BE

static short ReadInt16BE(in byte ptr)

Load a 16-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 2-byte location

Returns: Logical value in host order

[ 0x12, 0x34 ] == 0x1234

static short ReadInt16BE(ReadOnlySpan<byte> bytes)

Load a 16-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • bytes — Span of at least 2-bytes

Returns: Logical value in host order

[ 0x12, 0x34 ] == 0x1234

ReadInt16LE

static short ReadInt16LE(in byte ptr)

Load a 16-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 2-byte location

Returns: Logical value in host order

[ 0x34, 0x12 ] == 0x1234

ReadInt32BE

static int ReadInt32BE(in byte ptr)

Loads a 32-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of an 4-byte location

Returns: Logical value in host order

ReadInt32LE

static int ReadInt32LE(in byte ptr)

Loads a 32-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of an 4-byte location

Returns: Logical value in host order

[ 0x12, 0x34, 0x56, 0x78 ] => 0x12345678

ReadInt64BE

static long ReadInt64BE(in byte ptr)

Load a 64-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of an 8-byte location

Returns: Logical value in host order

([ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF) => 0x0123456789ABCDEF

ReadInt64LE

static long ReadInt64LE(in byte ptr)

Load a 64-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of an 8-byte location

Returns: Logical value in host order

[ 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x456, 0x23, 0x01) => 0x0123456789ABCDEF

ReadOrderedUInt32Unsafe

static Byte* ReadOrderedUInt32Unsafe(Byte* cursor, out uint value)

Read an unsigned 32-bit counter value encoded using the Compact Ordered Unsigned encoding

ReadOrderedUInt64Unsafe

static Byte* ReadOrderedUInt64Unsafe(Byte* cursor, out ulong value)

Read an unsigned 64-bit counter value encoded using the Compact Ordered Unsigned encoding

ReadUInt16BE

static ushort ReadUInt16BE(in byte ptr)

Load a 16-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 2-byte location

Returns: Logical value in host order

[ 0x12, 0x34 ] == 0x1234

static ushort ReadUInt16BE(ReadOnlySpan<byte> bytes)

Load a 16-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • bytes — Span of at least 2-bytes

Returns: Logical value in host order

[ 0x12, 0x34 ] == 0x1234

ReadUInt16LE

static ushort ReadUInt16LE(in byte ptr)

Load a 16-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 2-byte location

Returns: Logical value in host order

[ 0x34, 0x12 ] == 0x1234

ReadUInt32BE

static uint ReadUInt32BE(in byte ptr)

Loads a 32-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of an 4-byte location

Returns: Logical value in host order

ReadUInt32LE

static uint ReadUInt32LE(in byte ptr)

Loads a 32-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of an 4-byte location

Returns: Logical value in host order

[ 0x12, 0x34, 0x56, 0x78 ] => 0x12345678

ReadUInt64BE

static ulong ReadUInt64BE(in byte ptr)

Load a 64-bit integer from an in-memory buffer that holds a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of an 8-byte location

Returns: Logical value in host order

([ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF) => 0x0123456789ABCDEF

ReadUInt64LE

static ulong ReadUInt64LE(in byte ptr)

Load a 64-bit integer from an in-memory buffer that holds a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of an 8-byte location

Returns: Logical value in host order

[ 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x456, 0x23, 0x01) => 0x0123456789ABCDEF

ReadVarBytes

static int ReadVarBytes(ReadOnlySpan<byte> source, out ReadOnlySpan<byte> data)

Read a variable size byte sequence

This method performs bound checking.

ReadVarint32

static int ReadVarint32(ReadOnlySpan<byte> buffer, out uint value)

Reads a 7-bit encoded unsigned int (aka 'Varint32') from the buffer, and advances the cursor

Can read up to 5 bytes from the input

ReadVarint64

static int ReadVarint64(ReadOnlySpan<byte> source, out ulong value)

Reads a 7-bit encoded unsigned long (aka 'Varint64') from the buffer, and advances the cursor

Can read up to 10 bytes from the input

SizeOfCompact16

static uint SizeOfCompact16(ushort value)

Return the minimum number of bytes that hold the bits set (1) in a 32-bit unsigned integer

  • value — Number that needs to be encoded

Returns: Number of bytes needed (1-4)

SizeOfCompact32

static uint SizeOfCompact32(uint value)

Return the minimum number of bytes that hold the bits set (1) in a 32-bit unsigned integer

  • value — Number that needs to be encoded

Returns: Number of bytes needed (1-4)

SizeOfCompact64

static uint SizeOfCompact64(ulong value)

Return the minimum number of bytes that hold the bits set (1) in a 64-bit unsigned integer

  • value — Number that needs to be encoded

Returns: Number of bytes needed (1-8)

SizeOfOrderedUInt32

static int SizeOfOrderedUInt32(uint value)

Return the size (in bytes) that a 32-bit counter value would need with the Compact Order Unsigned encoding

  • value — Number that needs to be encoded

Returns: Number of bytes needed (1-5)

SizeOfOrderedUInt64

static int SizeOfOrderedUInt64(ulong value)

Return the size (in bytes) that a 64-bit counter value would need with the Compact Order Unsigned encoding

  • value — Number that needs to be encoded, between 0 and 2^60-1

Returns: Number of bytes needed (1-8), or 0 if the number would overflow (2^60 or greater)

SizeOfVarBytes

static uint SizeOfVarBytes(uint size)

Return the size (in bytes) that a variable-size array of bytes would need when encoded as a VarBytes

  • size — Size (in bytes) of the array

Returns: Number of bytes needed to encode the size of the array, and the array itself (1 + N <= size <= 5 + N)

static int SizeOfVarBytes(int size)

Return the size (in bytes) that a variable-size array of bytes would need when encoded as a VarBytes

  • size — Size (in bytes) of the array

Returns: Number of bytes needed to encode the size of the array, and the array itself (1 + N <= size <= 5 + N)

SizeOfVarInt

static uint SizeOfVarInt(uint value)

Return the size (in bytes) that a 32-bit number would need when encoded as a VarInt

  • value — Number that needs to be encoded

Returns: Number of bytes needed (1-5)

static uint SizeOfVarInt(ulong value)

Return the size (in bytes) that a 64-bit number would need when encoded as a VarInt

  • value — Number that needs to be encoded

Returns: Number of bytes needed (1-10)

static uint SizeOfVarInt(UInt128 value)

Return the size (in bytes) that a 128-bit number would need when encoded as a VarInt

  • value — Number that needs to be encoded

Returns: Number of bytes needed (1-19)

StoreInt16BE

static void StoreInt16BE(Void* ptr, short value)

Store a 16-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 2-byte location
  • value — Logical value to store in the buffer

0x1234 => [ 0x12, 0x34 ]

StoreInt16LE

static void StoreInt16LE(Void* ptr, short value)

Store a 16-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 2-byte location
  • value — Logical value to store in the buffer

[ 0x34, 0x12 ] == 0x1234

StoreInt24BE

static void StoreInt24BE(Void* ptr, int value)

Store a 24-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 3-byte location
  • value — Logical value to store in the buffer. Bits 24-31 are ignored

(ptr, 0x123456) => ptr[0] == 0x12, ptr[1] == 0x34, ptr[2] = 0x56

StoreInt24LE

static void StoreInt24LE(Void* ptr, int value)

Store a 24-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 3-byte location
  • value — Logical value to store in the buffer. Bits 24-31 are ignored

(ptr, 0x123456) => [ 0x12, 0x34, 0x56 ]

StoreInt32BE

static void StoreInt32BE(Void* ptr, int value)

Store a 32-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 4-byte location
  • value — Logical value to store in the buffer

(ptr, 0x12345678) => ptr[0] == 0x12, ptr[1] == 0x34, ptr[2] == 0x56, ptr[3] == 0x78

StoreInt32LE

static void StoreInt32LE(Void* ptr, int value)

Store a 32-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 4-byte location
  • value — Logical value to store in the buffer

(0x12345678) => ptr[0] == 0x78, ptr[1] == 0x56, ptr[2] == 0x34, ptr[3] == 0x12

StoreInt64BE

static void StoreInt64BE(Void* ptr, long value)

Store a 64-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of an 8-byte location
  • value — Logical value to store in the buffer

(ptr, 0x0123456789ABCDEF) => ptr[0] == 0x01, ptr[1] == 0x23, ptr[2] == 0x45, ptr[3] == 0x67, ..., ptr[7] == 0xEF

StoreInt64LE

static void StoreInt64LE(Void* ptr, long value)

Store a 64-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of an 8-byte location
  • value — Logical value to store in the buffer

0x0123456789ABCDEF => [ 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x456, 0x23, 0x01)

StoreUInt16BE

static void StoreUInt16BE(Void* ptr, ushort value)

Store a 16-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 2-byte location
  • value — Logical value to store in the buffer

0x1234 => [ 0x12, 0x34 ]

StoreUInt16LE

static void StoreUInt16LE(Void* ptr, ushort value)

Store a 16-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 2-byte location
  • value — Logical value to store in the buffer

[ 0x34, 0x12 ] == 0x1234

StoreUInt24BE

static void StoreUInt24BE(Void* ptr, uint value)

Store a 24-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 3-byte location
  • value — Logical value to store in the buffer. Bits 24-31 are ignored

(ptr, 0x123456) => ptr[0] == 0x12, ptr[1] == 0x34, ptr[2] = 0x56

StoreUInt24LE

static void StoreUInt24LE(Void* ptr, uint value)

Store a 24-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 3-byte location
  • value — Logical value to store in the buffer. Bits 24-31 are ignored

(ptr, 0x123456) => [ 0x12, 0x34, 0x56 ]

StoreUInt32BE

static void StoreUInt32BE(Void* ptr, uint value)

Store a 32-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 4-byte location
  • value — Logical value to store in the buffer

(ptr, 0x12345678) => ptr[0] == 0x12, ptr[1] == 0x34, ptr[2] == 0x56, ptr[3] == 0x78

StoreUInt32LE

static void StoreUInt32LE(Void* ptr, uint value)

Store a 32-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 4-byte location
  • value — Logical value to store in the buffer

(0x12345678) => ptr[0] == 0x78, ptr[1] == 0x56, ptr[2] == 0x34, ptr[3] == 0x12

StoreUInt64BE

static void StoreUInt64BE(Void* ptr, ulong value)

Store a 64-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of an 8-byte location
  • value — Logical value to store in the buffer

(ptr, 0x0123456789ABCDEF) => ptr[0] == 0x01, ptr[1] == 0x23, ptr[2] == 0x45, ptr[3] == 0x67, ..., ptr[7] == 0xEF

StoreUInt64LE

static void StoreUInt64LE(Void* ptr, ulong value)

Store a 64-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of an 8-byte location
  • value — Logical value to store in the buffer

0x0123456789ABCDEF => [ 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x456, 0x23, 0x01)

WriteCompact16BEUnsafe

static Byte* WriteCompact16BEUnsafe(Byte* ptr, ushort value)

Writes a big-endian 16 bits by using the minimum number of bytes

  • ptr — Pointer where to write the value
  • value — Value to write

Returns: Pointer advanced to the next position

Advances the pointer by 1 byte if is 0xFF or less; otherwise, advances the pointer by 2 bytes.

WriteCompact16Unsafe

static Byte* WriteCompact16Unsafe(Byte* ptr, ushort value)

Writes a little-endian 16 bits by using the minimum number of bytes

  • ptr — Pointer where to write the value
  • value — Value to write

Returns: Pointer advanced to the next position

Advances the pointer by 1 byte if is 0xFF or less; otherwise, advances the pointer by 2 bytes.

WriteCompact32BEUnsafe

static Byte* WriteCompact32BEUnsafe(Byte* ptr, uint value)

Writes a big-endian 32 bits by using the minimum number of bytes

  • ptr — Pointer where to write the value
  • value — Value to write

Returns: Pointer advanced to the next position

Advances the pointer by 1 to 4 bytes.

WriteCompact32Unsafe

static Byte* WriteCompact32Unsafe(Byte* ptr, uint value)

Writes a little-endian 32 bits by using the minimum number of bytes

  • ptr — Pointer where to write the value
  • value — Value to write

Returns: Pointer advanced to the next position

Advances the pointer by 1 to 4 bytes.

WriteCompact64BEUnsafe

static Byte* WriteCompact64BEUnsafe(Byte* ptr, ulong value)

Writes a big-endian 64 bits by using the minimum number of bytes

  • ptr — Pointer where to write the value
  • value — Value to write

Returns: Pointer advanced to the next position

Advances the pointer by 1 to 8 bytes.

WriteCompact64Unsafe

static Byte* WriteCompact64Unsafe(Byte* ptr, ulong value)

Writes a little-endian 64 bits by using the minimum number of bytes

  • ptr — Pointer where to write the value
  • value — Value to write

Returns: Pointer advanced to the next position

Advances the pointer by 1 to 8 bytes.

WriteInt16BE

static void WriteInt16BE(ref byte ptr, short value)

Store a 16-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 2-byte location
  • value — Logical value to store in the buffer

0x1234 => [ 0x12, 0x34 ]

static void WriteInt16BE(Span<byte> destination, short value)

Store a 16-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • destination — Memory address of a 2-byte location
  • value — Logical value to store in the buffer

0x1234 => [ 0x12, 0x34 ]

WriteInt16LE

static void WriteInt16LE(ref byte ptr, short value)

Store a 16-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 2-byte location
  • value — Logical value to store in the buffer

[ 0x34, 0x12 ] == 0x1234

WriteInt24LE

static void WriteInt24LE(ref byte ptr, int value)

Store a 24-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 3-byte location
  • value — Logical value to store in the buffer

(ref ptr, 0x123456) => [ 0x12, 0x34, 0x56 ]

WriteInt32BE

static void WriteInt32BE(ref byte ptr, int value)

Store a 32-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 4-byte location
  • value — Logical value to store in the buffer

[ 0x78, 0x56, 0x34, 0x12 ] == 0x12345678

WriteInt32LE

static void WriteInt32LE(ref byte ptr, int value)

Store a 32-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 4-byte location
  • value — Logical value to store in the buffer

[ 0x78, 0x56, 0x34, 0x12 ] == 0x12345678

WriteInt64BE

static void WriteInt64BE(ref byte ptr, long value)

Store a 32-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 4-byte location
  • value — Logical value to store in the buffer

[ 0x78, 0x56, 0x34, 0x12 ] == 0x12345678

WriteInt64LE

static void WriteInt64LE(ref byte ptr, long value)

Store a 64-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 4-byte location
  • value — Logical value to store in the buffer

[ 0x78, 0x56, 0x34, 0x12 ] == 0x12345678

WriteOrderedUInt32Unsafe

static Byte* WriteOrderedUInt32Unsafe(Byte* cursor, uint value)

Append an unsigned 32-bit counter value using a compact ordered encoding

  • cursor — Pointer to the next free byte in the buffer
  • value — Positive counter value

Returns: Pointer updated with the number of bytes written

Will write between 1 and 5 bytes

WriteOrderedUInt64Unsafe

static Byte* WriteOrderedUInt64Unsafe(Byte* cursor, ulong value)

Append an unsigned 64-bit counter value (up to 2^61-1) using the Compact Ordered Unsigned encoding

  • cursor — Pointer to the next free byte in the buffer
  • value — Positive counter value that must be between 0 and 2^61 - 1 (2,305,843,009,213,693,951 or 0x1FFFFFFFFFFFFFFF)

Returns: Pointer updated with the number of bytes written

Will write between 1 and 8 bytes

WriteUInt16BE

static void WriteUInt16BE(ref byte ptr, ushort value)

Store a 16-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 2-byte location
  • value — Logical value to store in the buffer

0x1234 => [ 0x12, 0x34 ]

static void WriteUInt16BE(Span<byte> destination, ushort value)

Store a 16-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • destination — Memory address of a 2-byte location
  • value — Logical value to store in the buffer

0x1234 => [ 0x12, 0x34 ]

WriteUInt16LE

static void WriteUInt16LE(ref byte ptr, ushort value)

Store a 16-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 2-byte location
  • value — Logical value to store in the buffer

[ 0x34, 0x12 ] == 0x1234

WriteUInt24LE

static void WriteUInt24LE(ref byte ptr, uint value)

Store a 24-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 3-byte location
  • value — Logical value to store in the buffer

(ref ptr, 0x123456) => [ 0x12, 0x34, 0x56 ]

WriteUInt32BE

static void WriteUInt32BE(ref byte ptr, uint value)

Store a 32-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 4-byte location
  • value — Logical value to store in the buffer

[ 0x78, 0x56, 0x34, 0x12 ] == 0x12345678

WriteUInt32LE

static void WriteUInt32LE(ref byte ptr, uint value)

Store a 32-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 4-byte location
  • value — Logical value to store in the buffer

[ 0x78, 0x56, 0x34, 0x12 ] == 0x12345678

WriteUInt64BE

static void WriteUInt64BE(ref byte ptr, ulong value)

Store a 32-bit integer in an in-memory buffer that must hold a value in Big-Endian ordering (also known as Network Order)

  • ptr — Memory address of a 4-byte location
  • value — Logical value to store in the buffer

[ 0x78, 0x56, 0x34, 0x12 ] == 0x12345678

WriteUInt64LE

static void WriteUInt64LE(ref byte ptr, ulong value)

Store a 64-bit integer in an in-memory buffer that must hold a value in Little-Endian ordering (also known as Host Order)

  • ptr — Memory address of a 4-byte location
  • value — Logical value to store in the buffer

[ 0x78, 0x56, 0x34, 0x12 ] == 0x12345678

WriteVarBytes

static int WriteVarBytes(Span<byte> destination, ReadOnlySpan<byte> data)

Append a variable size byte sequence, using the VarInt encoding

This method performs bound checking.

WriteVarInt32

static int WriteVarInt32(Span<byte> destination, uint value)

Append a variable sized number to the output buffer

  • destination — Buffer where to write the value
  • value — Value to write

Returns: Number of bytes written

Will write between 1 and 5 bytes

WriteVarInt64

static int WriteVarInt64(Span<byte> destination, ulong value)

Append a variable sized number to the output buffer

  • destination — Buffer where to write the value
  • value — Value to write

Returns: Number of bytes written

Will write between 1 and 10 bytes

WriteZeroTerminatedVarBytes

static int WriteZeroTerminatedVarBytes(Span<byte> destination, ReadOnlySpan<byte> data)

Append a variable size byte sequence with an extra 0 at the end, using the VarInt encoding

This method performs bound checking.