I need to get values in UInt16
and UInt64
as Byte[]
. At the moment I am using BitConverter.GetBytes
, but this method give
Now that .NET has added Span
support for better working with arrays, unmanaged memory, etc without excess allocations, they've also added System.Buffer.Binary.BinaryPrimitives.
This works as you would want, e.g. WriteUInt64BigEndian has this signature:
public static void WriteUInt64BigEndian (Span destination, ulong value);
which avoids allocating.