Table of Contents

BufferConverter Class

Definition

Namespace
Bodu.Extensions
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
BufferConverter.CopyTo.cs

Provides methods for copying and converting between raw byte arrays and arrays of unmanaged types.

public static class BufferConverter
Inheritance
BufferConverter
Inherited Members

Remarks

The BufferConverter class enables efficient reinterpretation of binary data as strongly-typed arrays without allocating additional memory or performing per-element conversions. Operations assume platform-native endianness.

Only types that are unmanaged (e.g., primitives, structs without references) are supported.

Methods

CopyTo<T>(byte[], int, T[], int)

Copies a specified number of elements of type T from a byte array into a target array, starting at index zero in the target array.

public static void CopyTo<T>(this byte[] sourceArray, int sourceIndex, T[] targetArray, int count) where T : unmanaged

Parameters

sourceArray byte[]

The source byte array.

sourceIndex int

The starting index in the sourceArray.

targetArray T[]

The target array to receive the elements.

count int

The number of elements of type T to copy.

Type Parameters

T

The unmanaged type to copy to.

CopyTo<T>(byte[], int, T[], int, int)

Copies a specified number of elements of type T from a byte array into a target array.

public static void CopyTo<T>(this byte[] sourceArray, int sourceIndex, T[] targetArray, int targetIndex, int count) where T : unmanaged

Parameters

sourceArray byte[]

The source byte array.

sourceIndex int

The starting index in the sourceArray.

targetArray T[]

The target array to receive the elements.

targetIndex int

The starting index in the targetArray.

count int

The number of elements of type T to copy.

Type Parameters

T

The unmanaged type to copy to.

Remarks

The method assumes that the byte array represents elements of type T using platform-native endianness.

Exceptions

ArgumentNullException

sourceArray or targetArray is null.

ArgumentOutOfRangeException

sourceIndex, targetIndex, or count is out of range.

ArgumentException

The specified range exceeds the bounds of the source or target arrays.

CopyTo<T>(Memory<byte>, Memory<T>, int)

Copies a specified number of elements of type T from a memory region of bytes into a memory region of T.

public static void CopyTo<T>(this Memory<byte> sourceMemory, Memory<T> targetMemory, int count) where T : unmanaged

Parameters

sourceMemory Memory<byte>

The source memory of bytes.

targetMemory Memory<T>

The target memory of T elements.

count int

The number of elements to copy.

Type Parameters

T

The unmanaged type to copy to.

CopyTo<T>(ReadOnlySpan<byte>, Span<T>, int)

Copies a specified number of elements of type T from a source span into a target span.

public static void CopyTo<T>(this ReadOnlySpan<byte> sourceSpan, Span<T> targetSpan, int count) where T : unmanaged

Parameters

sourceSpan ReadOnlySpan<byte>

The source span of bytes.

targetSpan Span<T>

The target span of T elements.

count int

The number of elements to copy.

Type Parameters

T

The unmanaged type to copy to.

Exceptions

ArgumentOutOfRangeException

The specified count exceeds the available elements in the source or target span.

ArgumentException

Thrown when a span or array argument does not meet a length or offset precondition.

CopyTo<T>(T, byte[], int)

Copies a single value of type T into a byte array at the specified index.

public static void CopyTo<T>(this T value, byte[] targetArray, int index) where T : unmanaged

Parameters

value T

The value to copy.

targetArray byte[]

The byte array to receive the value.

index int

The starting index in the targetArray.

Type Parameters

T

The unmanaged type to copy.

Exceptions

ArgumentNullException

targetArray is null.

ArgumentOutOfRangeException

index is negative or exceeds the available space in targetArray.

Read<T>(byte[], int)

Reads a single element of type T from a byte array at a specified index.

public static T Read<T>(this byte[] sourceArray, int index) where T : unmanaged

Parameters

sourceArray byte[]

The source byte array.

index int

The starting index in the sourceArray.

Returns

T

The value of type T read from the specified location.

Type Parameters

T

The unmanaged type to read.

Remarks

The method assumes that the byte array represents the value using platform-native endianness.

Exceptions

ArgumentNullException

sourceArray is null.

ArgumentOutOfRangeException

index is out of range or insufficient bytes remain.

Read<T>(ReadOnlySpan<byte>)

Reads a single element of type T from a span of bytes.

public static T Read<T>(this ReadOnlySpan<byte> sourceSpan) where T : unmanaged

Parameters

sourceSpan ReadOnlySpan<byte>

The source span of bytes.

Returns

T

The value of type T read from the span.

Type Parameters

T

The unmanaged type to read.

Remarks

The method assumes that the span represents the value using platform-native endianness.

Exceptions

ArgumentOutOfRangeException

The span length is insufficient to represent a value of type T.

ArgumentException

Thrown when a span or array argument does not meet a length or offset precondition.

SwapEndian(ReadOnlySpan<byte>, Span<byte>, int)

Copies data from a source span to a destination span, swapping the byte order (endianness) of each element of size elementSize bytes.

public static void SwapEndian(ReadOnlySpan<byte> source, Span<byte> destination, int elementSize)

Parameters

source ReadOnlySpan<byte>

The source span of bytes.

destination Span<byte>

The destination span of bytes to receive the endian-swapped elements.

elementSize int

The size, in bytes, of each element whose byte order should be swapped.

Remarks

This method reverses the byte order of each element individually while copying from source to destination. It assumes platform-native layout and performs the swap without allocating additional memory.

Exceptions

ArgumentOutOfRangeException

Thrown if elementSize is less than or equal to 1.

ArgumentException

Thrown if the source or destination spans do not have a length that is a multiple of elementSize, or if the destination is too small to receive the swapped data.

SwapEndian<T>(Span<T>)

Swaps the byte order (endianness) of each element of type T in the specified span.

public static void SwapEndian<T>(this Span<T> span) where T : unmanaged

Parameters

span Span<T>

The span containing elements to swap in place.

Type Parameters

T

The unmanaged type whose byte order will be swapped.

Remarks

This method reverses the byte order of each element in the span individually, performing the operation in place without allocating memory. No operation is performed for types with a size of one byte or less. The swap assumes platform-native layout of T. The operation is safe for empty spans and spans containing only one element.

ToArray<T>(byte[], int, int)

Converts a specified number of elements of type T from a byte array into a new array of type T.

public static T[] ToArray<T>(this byte[] sourceArray, int sourceIndex, int count) where T : unmanaged

Parameters

sourceArray byte[]

The source byte array.

sourceIndex int

The starting index in the sourceArray.

count int

The number of elements of type T to copy.

Returns

T[]

A new array of T elements.

Type Parameters

T

The unmanaged type to copy to.

Remarks

The method assumes that the byte array represents elements of type T using platform-native endianness.

Exceptions

ArgumentNullException

sourceArray is null.

ArgumentOutOfRangeException

sourceIndex or count is out of range.

ArgumentException

The specified range exceeds the bounds of the source array.

ToArray<T>(ReadOnlySpan<byte>, int)

Converts a specified number of elements of type T from a span of bytes into a new array of type T.

public static T[] ToArray<T>(this ReadOnlySpan<byte> sourceSpan, int count) where T : unmanaged

Parameters

sourceSpan ReadOnlySpan<byte>

The source span of bytes.

count int

The number of elements to copy.

Returns

T[]

A new array of T elements.

Type Parameters

T

The unmanaged type to copy to.

Remarks

The method assumes that the span represents elements of type T using platform-native endianness.

Exceptions

ArgumentOutOfRangeException

count is negative or exceeds the available bytes.

ArgumentException

Thrown when a span or array argument does not meet a length or offset precondition.

Applies to

ProductVersions
.NET8, 10