From 7ab6d5a64944247f0a3502ca39492974670806e4 Mon Sep 17 00:00:00 2001 From: Stoyan Zlatev Date: Mon, 15 Jul 2024 10:20:07 +0200 Subject: [PATCH] Bytes extension methods --- .../BytesExtensions.cs | 159 ++++++++++++++++++ .../Common.Utils.Extensions.csproj | 51 ++++++ .../Properties/AssemblyInfo.cs | 36 ++++ 3 files changed, 246 insertions(+) create mode 100644 Common/Utils/Common.Utils.Extensions/BytesExtensions.cs create mode 100644 Common/Utils/Common.Utils.Extensions/Common.Utils.Extensions.csproj create mode 100644 Common/Utils/Common.Utils.Extensions/Properties/AssemblyInfo.cs diff --git a/Common/Utils/Common.Utils.Extensions/BytesExtensions.cs b/Common/Utils/Common.Utils.Extensions/BytesExtensions.cs new file mode 100644 index 00000000..8cdb60a8 --- /dev/null +++ b/Common/Utils/Common.Utils.Extensions/BytesExtensions.cs @@ -0,0 +1,159 @@ +namespace Common.Utils.Extensions +{ + using System; + + public static class BytesExtensions + { + public const Int32 Int16Size = sizeof(Int16); + public const Int32 Int32Size = sizeof(Int32); + public const Int32 Int64Size = sizeof(Int64); + + public const Int32 UInt16Size = sizeof(UInt16); + public const Int32 UInt32Size = sizeof(UInt32); + public const Int32 UInt64Size = sizeof(UInt64); + + public const Int32 SingleSize = sizeof(Single); + public const Int32 DoubleSize = sizeof(Double); + + public static Byte[] GetBits(this Byte _byte) + { + var bits = new Byte[8]; + + for (var i = 0; i < 8; i++) + { + bits[i] = (Byte)((_byte >> i) & 1); + } + + return bits; + } + + public static void SetBits(this Byte value, Byte[] bits, Int32 start, Int32 count) + { + for (var i = 0; i < count; i++) + { + bits[start + i] = (Byte)((value >> i) & 1); + } + } + + public static Byte ToByte(this Byte[] bits) + { + var _byte = default(Int32); + + for (var i = 0; i < 8; i++) + { + _byte |= bits[i] << i; + } + + return (Byte)_byte; + } + + public static Byte ToByte(this Byte[] bits, Int32 start, Int32 count) + { + var sum = 0; + + for (var i = 0; i < count; i++) + { + sum |= bits[start + i] << i; + } + + return (Byte)sum; + } + + public static Byte[] GetReversed(this Byte[] bytes, Int32 start, Int32 size) + { + var _bytes = new Byte[size]; + + Array.Copy(bytes, start, _bytes, 0, size); + Array.Reverse(_bytes); + + return _bytes; + } + + public static Int16 ToInt16BigEndian(this Byte[] bytes, Int32 start) + { + var _bytes = bytes.GetReversed(start, Int16Size); + + return BitConverter.ToInt16(_bytes, 0); + } + + public static Int32 ToInt32BigEndian(this Byte[] bytes, Int32 start) + { + var _bytes = bytes.GetReversed(start, Int32Size); + + return BitConverter.ToInt32(_bytes, 0); + } + + public static Int64 ToInt64BigEndian(this Byte[] bytes, Int32 start) + { + var _bytes = bytes.GetReversed(start, Int64Size); + + return BitConverter.ToInt64(_bytes, 0); + } + + public static UInt16 ToUInt16BigEndian(this Byte[] bytes, Int32 start) + { + var _bytes = bytes.GetReversed(start, UInt16Size); + + return BitConverter.ToUInt16(_bytes, 0); + } + + public static UInt32 ToUInt32BigEndian(this Byte[] bytes, Int32 start) + { + var _bytes = bytes.GetReversed(start, UInt32Size); + + return BitConverter.ToUInt32(_bytes, 0); + } + + public static UInt64 ToUInt64BigEndian(this Byte[] bytes, Int32 start) + { + var _bytes = bytes.GetReversed(start, UInt64Size); + + return BitConverter.ToUInt64(_bytes, 0); + } + + public static Single ToSingleBigEndian(this Byte[] bytes, Int32 start) + { + var _bytes = bytes.GetReversed(start, SingleSize); + + return BitConverter.ToSingle(_bytes, 0); + } + + public static Double ToDobleBigEndian(this Byte[] bytes, Int32 start) + { + var _bytes = bytes.GetReversed(start, DoubleSize); + + return BitConverter.ToDouble(_bytes, 0); + } + + public static Byte[] Reverse(this Byte[] bytes) + { + Array.Reverse(bytes); + + return bytes; + } + + public static Byte[] ToBigEndianBytes(this Int16 value) + => BitConverter.GetBytes(value).Reverse(); + + public static Byte[] ToBigEndianBytes(this Int32 value) + => BitConverter.GetBytes(value).Reverse(); + + public static Byte[] ToBigEndianBytes(this Int64 value) + => BitConverter.GetBytes(value).Reverse(); + + public static Byte[] ToBigEndianBytes(this UInt16 value) + => BitConverter.GetBytes(value).Reverse(); + + public static Byte[] ToBigEndianBytes(this UInt32 value) + => BitConverter.GetBytes(value).Reverse(); + + public static Byte[] ToBigEndianBytes(this UInt64 value) + => BitConverter.GetBytes(value).Reverse(); + + public static Byte[] ToBigEndianBytes(this Single value) + => BitConverter.GetBytes(value).Reverse(); + + public static Byte[] ToBigEndianBytes(this Double value) + => BitConverter.GetBytes(value).Reverse(); + } +} diff --git a/Common/Utils/Common.Utils.Extensions/Common.Utils.Extensions.csproj b/Common/Utils/Common.Utils.Extensions/Common.Utils.Extensions.csproj new file mode 100644 index 00000000..3d381444 --- /dev/null +++ b/Common/Utils/Common.Utils.Extensions/Common.Utils.Extensions.csproj @@ -0,0 +1,51 @@ + + + + + Debug + AnyCPU + {05F0CCAF-9F2B-45F9-A7F8-12F29AD182F3} + Library + Common.Utils.Extensions + Common.Utils.Extensions + v4.0 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Common/Utils/Common.Utils.Extensions/Properties/AssemblyInfo.cs b/Common/Utils/Common.Utils.Extensions/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..63d183b2 --- /dev/null +++ b/Common/Utils/Common.Utils.Extensions/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Common.Utils.Extensions")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Common.Utils.Extensions")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("05f0ccaf-9f2b-45f9-a7f8-12f29ad182f3")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]