Utilities to read/write primitives from/to spans easily. Uses the C# Span struct.
Install the Nuget Package
Reading/writing spans can be a chore, keeping track of where you're up to reading a span. This lib lets you simplify this:
var one = BinaryPrimitives.ReadUInt16LittleEndian(span.Slice(3, 2));
var two = BinaryPrimitives.ReadUInt32LittleEndian(span.Slice(5, 4));
var three = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(9, 2));
to this:
int cursor = 0;
var one = span.ReadUInt16LittleEndian(ref cursor);
var two = span.ReadUInt32LittleEndian(ref cursor);
var three = span.ReadUInt16BigEndian(ref cursor);
or:
var spanReader = new SpanReader(span);
var one = spanReader.ReadUInt16LittleEndian();
var two = spanReader.ReadUInt32LittleEndian();
var three = spanReader.ReadUInt16BigEndian();
This lib is based around the System.Buffers.Binary.BinaryPrimitives
class, with some other methods sprinkled in for some spice.
SpanReader
for reading a spanSpanWriter
for creating and writing to a span; initialize withsize
orMemory<byte>
- A tonne of extension methods, applicable to
Span<byte>
andReadOnlySpan<byte>
- all ripped straight fromSystem.Buffers.Binary.BinaryPrimitives
, as well as: span.ReadSpan()
- same as slice, but keeps track of where you're up tospan.ReadASCIIString()
- does what it says on the packetspan.ReadUTF8String()
- I think you get the drift