skyscraper8/skyscraper8/Skyscraper/SpanByteExtensions.cs
feyris-tan 6c36c100e8 Ditto!
2026-04-15 18:48:26 +02:00

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);
}