diff --git a/Utilities/ByteUtils/BigEndianReader.cs b/Utilities/ByteUtils/BigEndianReader.cs index 833ed7a..7ef6034 100644 --- a/Utilities/ByteUtils/BigEndianReader.cs +++ b/Utilities/ByteUtils/BigEndianReader.cs @@ -18,6 +18,17 @@ namespace Utilities return BigEndianConverter.ToUInt16(buffer, offset - 2); } + public static uint ReadUInt24(byte[] buffer, int offset) + { + return (uint)((buffer[offset + 0] << 16) | (buffer[offset + 1] << 8) | (buffer[offset + 2] << 0)); + } + + public static uint ReadUInt24(byte[] buffer, ref int offset) + { + offset += 3; + return ReadUInt24(buffer, offset - 3); + } + public static int ReadInt32(byte[] buffer, ref int offset) { offset += 4; @@ -62,6 +73,13 @@ namespace Utilities return BigEndianConverter.ToUInt16(buffer, 0); } + public static uint ReadUInt24(Stream stream) + { + byte[] buffer = new byte[4]; + stream.Read(buffer, 1, 3); + return BigEndianConverter.ToUInt32(buffer, 0); + } + public static int ReadInt32(Stream stream) { byte[] buffer = new byte[4]; diff --git a/Utilities/ByteUtils/BigEndianWriter.cs b/Utilities/ByteUtils/BigEndianWriter.cs index e7ef51a..e6cb787 100644 --- a/Utilities/ByteUtils/BigEndianWriter.cs +++ b/Utilities/ByteUtils/BigEndianWriter.cs @@ -30,6 +30,18 @@ namespace Utilities offset += 2; } + public static void WriteUInt24(byte[] buffer, int offset, uint value) + { + byte[] bytes = BigEndianConverter.GetBytes(value); + Array.Copy(bytes, 1, buffer, offset, 3); + } + + public static void WriteUInt24(byte[] buffer, ref int offset, uint value) + { + WriteUInt24(buffer, offset, value); + offset += 3; + } + public static void WriteInt32(byte[] buffer, int offset, int value) { byte[] bytes = BigEndianConverter.GetBytes(value); @@ -102,6 +114,12 @@ namespace Utilities stream.Write(bytes, 0, bytes.Length); } + public static void WriteUInt24(Stream stream, uint value) + { + byte[] bytes = BigEndianConverter.GetBytes(value); + stream.Write(bytes, 1, 3); + } + public static void WriteInt32(Stream stream, int value) { byte[] bytes = BigEndianConverter.GetBytes(value);