BitConverter.GetBytes in place

后端 未结 5 863
-上瘾入骨i
-上瘾入骨i 2021-01-20 05:17

I need to get values in UInt16 and UInt64 as Byte[]. At the moment I am using BitConverter.GetBytes, but this method give

5条回答
  •  一生所求
    2021-01-20 05:49

    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.

提交回复
热议问题