Skip to content

Instantly share code, notes, and snippets.

@Drawaes
Created September 17, 2016 13:20
Show Gist options
  • Save Drawaes/5ec402fc7dfecd29208fe6aa8a2ebe2e to your computer and use it in GitHub Desktop.
Save Drawaes/5ec402fc7dfecd29208fe6aa8a2ebe2e to your computer and use it in GitHub Desktop.
Reader for BigEndian
public class BigEndianReaders
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ReadUInt16(ref ReadableBuffer buffer)
{
var returnValue = PeekUInt16(buffer);
buffer = buffer.Slice(2);
return returnValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short ReadInt16(ref ReadableBuffer buffer)
{
return (short)ReadUInt16(ref buffer);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort PeekUInt16(ReadableBuffer buffer)
{
var x = buffer.Read<ushort>();
x = (ushort)((x << 8) | (x >> 8));
return x;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ReadInt32(ref ReadableBuffer buffer)
{
return (int) ReadUInt32(ref buffer);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReadUInt32(ref ReadableBuffer buffer)
{
var x = buffer.Read<uint>();
x = ((x >> 24) | ((x >> 8) & 0x0000FF00) | ((x << 8) & 0x00FF0000) | (x << 24));
buffer = buffer.Slice(4);
return x;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static sbyte ReadInt8(ref ReadableBuffer buffer)
{
var x = buffer.Read<sbyte>();
buffer = buffer.Slice(1);
return x;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte ReadUInt8(ref ReadableBuffer buffer)
{
var x = buffer.Read<byte>();
buffer = buffer.Slice(1);
return x;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment