35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using System;
|
|
|
|
public static class SpanByteExtensions
|
|
{
|
|
public static uint ReadUInt32LittleEndian(this ReadOnlySpan<byte> span, int offset)
|
|
{
|
|
if ((uint)offset > (uint)(span.Length - 4))
|
|
throw new ArgumentOutOfRangeException(nameof(offset));
|
|
|
|
return (uint)(
|
|
span[offset] |
|
|
(span[offset + 1] << 8) |
|
|
(span[offset + 2] << 16) |
|
|
(span[offset + 3] << 24));
|
|
}
|
|
|
|
public static uint ReadUInt32BigEndian(this ReadOnlySpan<byte> span, int offset)
|
|
{
|
|
if ((uint)offset > (uint)(span.Length - 4))
|
|
throw new ArgumentOutOfRangeException(nameof(offset));
|
|
|
|
return (uint)(
|
|
(span[offset] << 24) |
|
|
(span[offset + 1] << 16) |
|
|
(span[offset + 2] << 8) |
|
|
span[offset + 3]);
|
|
}
|
|
|
|
public static uint ReadUInt32LittleEndian(this Span<byte> span, int offset)
|
|
=> ReadUInt32LittleEndian((ReadOnlySpan<byte>)span, offset);
|
|
|
|
public static uint ReadUInt32BigEndian(this Span<byte> span, int offset)
|
|
=> ReadUInt32BigEndian((ReadOnlySpan<byte>)span, offset);
|
|
}
|