namespace Common.Utils.Extensions { using System; using System.Collections.Generic; public static class BytesExtensions { public static Int32 Int32BigEndian(this Byte[] bytes, Int32 start) { var _bytes = new Byte[4]; if (bytes.Length >= start + 4) { Array.Copy(bytes, start, _bytes, 0, 4); Array.Reverse(_bytes); } return BitConverter.ToInt32(_bytes, 0); } public static Byte Byte(this Byte[] bytes, Int32 start) { var _byte = default(Byte); if (bytes.Length > start) { _byte = bytes[start]; } return _byte; } 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[] GetBytes(this String hexString) { var bytes = default(Byte[]); if (!string.IsNullOrWhiteSpace(hexString)) { hexString = hexString.ToUpper(); var stringLength = hexString.Length; var byteList = new List(); var hexByte = default(String); for (var charIndex = 0; charIndex < stringLength; charIndex++) { var _char = hexString[charIndex]; if (('0' <= _char && _char <= '9') || ('A' <= _char && _char <= 'F')) { hexByte += _char; } if (hexByte?.Length == 2) { var _byte = Convert.ToByte(hexByte, 16); hexByte = default(String); byteList.Add(_byte); } } if (hexByte is null) { bytes = byteList.ToArray(); } } return bytes; } 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(); } }