BufferConverter Class
Definition
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
sourceArraybyte[]The source byte array.
sourceIndexintThe starting index in the
sourceArray.targetArrayT[]The target array to receive the elements.
countintThe number of elements of type
Tto copy.
Type Parameters
TThe 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
sourceArraybyte[]The source byte array.
sourceIndexintThe starting index in the
sourceArray.targetArrayT[]The target array to receive the elements.
targetIndexintThe starting index in the
targetArray.countintThe number of elements of type
Tto copy.
Type Parameters
TThe unmanaged type to copy to.
Remarks
The method assumes that the byte array represents elements of type T using
platform-native endianness.
Exceptions
- ArgumentNullException
sourceArrayortargetArrayis null.- ArgumentOutOfRangeException
sourceIndex,targetIndex, orcountis 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
sourceMemoryMemory<byte>The source memory of bytes.
targetMemoryMemory<T>The target memory of
Telements.countintThe number of elements to copy.
Type Parameters
TThe 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
sourceSpanReadOnlySpan<byte>The source span of bytes.
targetSpanSpan<T>The target span of
Telements.countintThe number of elements to copy.
Type Parameters
TThe unmanaged type to copy to.
Exceptions
- ArgumentOutOfRangeException
The specified
countexceeds 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
valueTThe value to copy.
targetArraybyte[]The byte array to receive the value.
indexintThe starting index in the
targetArray.
Type Parameters
TThe unmanaged type to copy.
Exceptions
- ArgumentNullException
targetArrayis null.- ArgumentOutOfRangeException
indexis negative or exceeds the available space intargetArray.
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
Returns
- T
The value of type
Tread from the specified location.
Type Parameters
TThe unmanaged type to read.
Remarks
The method assumes that the byte array represents the value using platform-native endianness.
Exceptions
- ArgumentNullException
sourceArrayis null.- ArgumentOutOfRangeException
indexis 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
sourceSpanReadOnlySpan<byte>The source span of bytes.
Returns
- T
The value of type
Tread from the span.
Type Parameters
TThe 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
sourceReadOnlySpan<byte>The source span of bytes.
destinationSpan<byte>The destination span of bytes to receive the endian-swapped elements.
elementSizeintThe 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
elementSizeis 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
spanSpan<T>The span containing elements to swap in place.
Type Parameters
TThe 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
sourceArraybyte[]The source byte array.
sourceIndexintThe starting index in the
sourceArray.countintThe number of elements of type
Tto copy.
Returns
- T[]
A new array of
Telements.
Type Parameters
TThe unmanaged type to copy to.
Remarks
The method assumes that the byte array represents elements of type T using
platform-native endianness.
Exceptions
- ArgumentNullException
sourceArrayis null.- ArgumentOutOfRangeException
sourceIndexorcountis 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
sourceSpanReadOnlySpan<byte>The source span of bytes.
countintThe number of elements to copy.
Returns
- T[]
A new array of
Telements.
Type Parameters
TThe unmanaged type to copy to.
Remarks
The method assumes that the span represents elements of type T using platform-native
endianness.
Exceptions
- ArgumentOutOfRangeException
countis negative or exceeds the available bytes.- ArgumentException
Thrown when a span or array argument does not meet a length or offset precondition.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |