{"id":354,"date":"2021-01-02T07:35:38","date_gmt":"2021-01-02T07:35:38","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=354"},"modified":"2021-05-04T11:13:00","modified_gmt":"2021-05-04T11:13:00","slug":"getbytesclass-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2021\/01\/02\/getbytesclass-cs\/","title":{"rendered":"GetBytesClass.cs"},"content":{"rendered":"\n<p>GetBytes Arrays, Classes, Structures, Primitives<\/p>\n\n\n\n<p>Updated: Apr-15,2021<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Collections;\nusing System.Collections.Generic;\nusing System.IO;\nusing System.Linq;\nusing System.Numerics;\nusing System.Reflection;\nusing System.Runtime.InteropServices;\nusing System.Runtime.Serialization.Formatters.Binary;\nusing System.Security;\nusing System.Security.Cryptography;\nusing System.Text;\n\/\/\/ &lt;summary>\n\/\/\/     Name: GetBytesClass.cs\n\/\/\/     Date: Dec-01,2020\n\/\/\/     Revised: Mar-11,2021\n\/\/\/     Revised: Apr-15, 2021\n\/\/\/ &lt;\/summary>\npublic static class GetBytesClass\n{\n    private static readonly int                      _charSize = sizeof(char);\n    private static readonly BinaryFormatter          Formatter = new();\n    private static readonly MonitorActionFuncWrapper _maf      = new();\n    private static readonly SHA512Managed            _hash     = new();\n    public static byte[] GetHashCodeU&lt;T>(this T obj, int bitWidth)\n    {\n        return _maf.Lock(_hash, () =>\n        {\n            var buf = GetBytes(obj);\n            if (bitWidth &lt; 16 || bitWidth > 512)\n                throw new ArgumentException($\"Bit Width {bitWidth} must be between 16 and 512.\");\n            return _hash.ComputeHash(buf).SubByte(0, bitWidth >> 3);\n        });\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get a byte array representation of most any object.\n    \/\/\/ &lt;\/summary>\n    [SecurityCritical]\n    public static byte[] GetBytes(this object obj)\n    {\n        if (obj == null)\n            throw new ArgumentException(\"Object cannot be null.\");\n        var ltype = obj.GetType();\n        switch (ltype.Name.Trim('[', ']'))\n        {\n            case \"Byte\":\n                return !ltype.IsArray ? new[] {(byte) obj} : (byte[]) obj;\n            case \"Boolean\":\n                return !ltype.IsArray ? GetBytes((bool) obj) : GetBytes((bool[]) obj);\n            case \"SByte\":\n                return !ltype.IsArray ? GetBytes((sbyte) obj) : GetBytes((sbyte[]) obj);\n            case \"Char\":\n                return !ltype.IsArray ? GetBytes((char) obj) : GetBytes((char[]) obj);\n            case \"Int16\":\n                return !ltype.IsArray ? GetBytes((short) obj) : GetBytes((short[]) obj);\n            case \"UInt16\":\n                return !ltype.IsArray ? GetBytes((ushort) obj) : GetBytes((ushort[]) obj);\n            case \"Int32\":\n                return !ltype.IsArray ? GetBytes((int) obj) : GetBytes((int[]) obj);\n            case \"UInt32\":\n                return !ltype.IsArray ? GetBytes((uint) obj) : GetBytes((uint[]) obj);\n            case \"Int64\":\n                return !ltype.IsArray ? GetBytes((long) obj) : GetBytes((long[]) obj);\n            case \"UInt64\":\n                return !ltype.IsArray ? GetBytes((ulong) obj) : GetBytes((ulong[]) obj);\n            case \"Single\":\n                return !ltype.IsArray ? GetBytes((float) obj) : GetBytes((float[]) obj);\n            case \"Double\":\n                return !ltype.IsArray ? GetBytes((double) obj) : GetBytes((double[]) obj);\n            case \"String\":\n                return !ltype.IsArray ? GetBytes((string) obj) : GetBytes((string[]) obj);\n            case \"Decimal\":\n                return !ltype.IsArray ? GetBytes((decimal) obj) : GetBytes((decimal[]) obj);\n            case \"DateTime\":\n                return !ltype.IsArray ? GetBytes((DateTime) obj) : GetBytes((DateTime[]) obj);\n            case \"SecureString\":\n                return !ltype.IsArray ? GetBytes((SecureString) obj) : GetBytes((SecureString[]) obj);\n            case \"xIntX\":\n                return !ltype.IsArray ? xIntX.GetBytesInt((xIntX) obj) : GetBytes((xIntX[]) obj);\n            case \"BigInteger\":\n                return !ltype.IsArray ? ((BigInteger) obj).ToByteArray() : GetBytes((BigInteger[]) obj);\n        }\n        return ltype.IsArray ? GetUnClassifiedBytesArray(obj) : GetUnClassifiedBytes(obj, ltype);\n    }\n    private static bool IsValidPrimitive(Type ltype)\n    {\n        switch (ltype.Name.Trim('[', ']'))\n        {\n            case \"Byte\":\n            case \"Boolean\":\n            case \"SByte\":\n            case \"Char\":\n            case \"Int16\":\n            case \"UInt16\":\n            case \"Int32\":\n            case \"UInt32\":\n            case \"Int64\":\n            case \"UInt64\":\n            case \"Single\":\n            case \"Double\":\n            case \"String\":\n            case \"Decimal\":\n            case \"DateTime\":\n                return true;\n            default:\n                return false;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single boolean object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(bool value)\n    {\n        return new[] {value ? (byte) 1 : (byte) 0};\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of boolean objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(bool[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (bool[]) object cannot be null.\");\n        var seed = new byte[0];\n        return value.Aggregate(seed, (Current, bl) => Current.Add(bl.GetBytes()));\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single byte object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(byte value)\n    {\n        return new[] {value};\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a sbyte short object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    [SecuritySafeCritical]\n    private static unsafe byte[] GetBytes(sbyte value)\n    {\n        var numArray = new byte[1];\n        fixed (byte* ptr = numArray)\n        {\n            *(sbyte*) ptr = value;\n        }\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of sbyte objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(sbyte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (sbyte[]) object cannot be null.\");\n        var numArray = new byte[value.Length];\n        Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single short object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    [SecuritySafeCritical]\n    private static unsafe byte[] GetBytes(short value)\n    {\n        var numArray = new byte[2];\n        fixed (byte* ptr = numArray)\n        {\n            *(short*) ptr = value;\n        }\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of short objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(short[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (short[]) object cannot be null.\");\n        var numArray = new byte[value.Length * 2];\n        Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single unsigned short object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(ushort value)\n    {\n        return ((short) value).GetBytes();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of unsigned short objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(ushort[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (ushort[]) object cannot be null.\");\n        var numArray = new byte[value.Length * 2];\n        Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single character object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(char value)\n    {\n        return ((short) value).GetBytes();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of character objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(char[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (char[]) object cannot be null.\");\n        var numArray = new byte[value.Length * 2];\n        Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single integer object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    [SecuritySafeCritical]\n    private static unsafe byte[] GetBytes(int value)\n    {\n        var numArray = new byte[4];\n        fixed (byte* ptr = numArray)\n        {\n            *(int*) ptr = value;\n        }\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of integer objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(int[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (int[]) object cannot be null.\");\n        var numArray = new byte[value.Length * 4];\n        Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single unsigned integer object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(uint value)\n    {\n        return ((int) value).GetBytes();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of unsigned integer objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(uint[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (uint[]) object cannot be null.\");\n        var numArray = new byte[value.Length * 4];\n        Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single long object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static unsafe byte[] GetBytes(long value)\n    {\n        var numArray = new byte[8];\n        fixed (byte* ptr = numArray)\n        {\n            *(long*) ptr = value;\n        }\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of long objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(long[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (long[]) object cannot be null.\");\n        var numArray = new byte[value.Length * 8];\n        Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of long objects with index and count\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static byte[] GetBytes(long value, int sIndex = 0, int count = 8)\n    {\n        if (count > 8)\n            throw new Exception(\"Size cannot exceed 8 bytes.\");\n        return value.GetBytes().SubArray(sIndex, count);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single unsigned long object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(ulong value)\n    {\n        return GetBytes((long) value);\n    }\n    public static byte[] GetBytes(this ulong value, int sIndex = 0, int count = 8)\n    {\n        if (count > 8)\n            throw new Exception(\"Size cannot exceed 8 bytes.\");\n        return ((long) value).GetBytes().SubArray(sIndex, count);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of unsigned long objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(ulong[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (ulong[]) object cannot be null.\");\n        var numArray = new byte[value.Length * 8];\n        Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single float object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    [SecuritySafeCritical]\n    private static unsafe byte[] GetBytes(float value)\n    {\n        return (*(int*) &amp;value).GetBytes();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of float objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(float[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (float[]) object cannot be null.\");\n        var numArray = new byte[value.Length * 4];\n        Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single double object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static unsafe byte[] GetBytes(double value)\n    {\n        return (*(long*) &amp;value).GetBytes();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of double objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(double[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (double[]) object cannot be null.\");\n        var numArray = new byte[value.Length * 8];\n        Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single decimal object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static unsafe byte[] GetBytes(decimal value)\n    {\n        var array = new byte[16];\n        fixed (byte* bp = array)\n        {\n            *(decimal*) bp = value;\n        }\n        return array;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single DateTime object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(DateTime value)\n    {\n        return value.Ticks.GetBytes();\n    }\n    private static byte[] GetBytes(DateTime[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (DateTime[]) object cannot be null.\");\n        var sodt = 0;\n        unsafe\n        {\n            sodt = sizeof(DateTime);\n        }\n        var numArray = new byte[value.Length * sodt];\n        for (var i = 0; i &lt; value.Length; i++)\n        {\n            var dba = value[i].GetBytes();\n            Buffer.BlockCopy(dba, 0, numArray, i * sodt, sodt);\n        }\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of decimal objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(decimal[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (decimal[]) object cannot be null.\");\n        var numArray = new byte[value.Length * 16];\n        for (var i = 0; i &lt; value.Length; i++)\n        {\n            var dba = value[i].GetBytes();\n            Buffer.BlockCopy(dba, 0, numArray, i * 16, 16);\n        }\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single string object using a specified Encoding.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static byte[] GetBytes(this string value, Encoding enc = null)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (string) object cannot be null.\");\n        if (enc == null)\n            return Encoding.ASCII.GetBytes(value);\n        switch (enc)\n        {\n            case ASCIIEncoding AsciiEncoding:\n            {\n                return Encoding.ASCII.GetBytes(value);\n            }\n            case UnicodeEncoding UnicodeEncoding:\n            {\n                var ba  = Encoding.Unicode.GetBytes(value);\n                var pre = new byte[] {0xff, 0xfe};\n                var ra  = new byte[ba.Length + 2];\n                Array.Copy(pre, 0, ra, 0, 2);\n                Array.Copy(ba,  0, ra, 2, ba.Length);\n                return ra;\n            }\n            case UTF32Encoding Utf32Encoding:\n            {\n                var ba  = Encoding.UTF32.GetBytes(value);\n                var pre = new byte[] {0xff, 0xfe, 0, 0};\n                var ra  = new byte[ba.Length + 4];\n                Array.Copy(pre, 0, ra, 0, 4);\n                Array.Copy(ba,  0, ra, 4, ba.Length);\n                return ra;\n            }\n            case UTF7Encoding Utf7Encoding:\n            {\n                var ba  = Encoding.UTF7.GetBytes(value);\n                var pre = new byte[] {0x2b, 0x2f, 0x76};\n                var ra  = new byte[ba.Length + 3];\n                Array.Copy(pre, 0, ra, 0, 3);\n                Array.Copy(ba,  0, ra, 3, ba.Length);\n                return ra;\n            }\n            case UTF8Encoding Utf8Encoding:\n            {\n                var ba  = Encoding.UTF8.GetBytes(value);\n                var pre = new byte[] {0xef, 0xbb, 0xbf};\n                var ra  = new byte[ba.Length + 3];\n                Array.Copy(pre, 0, ra, 0, 3);\n                Array.Copy(ba,  0, ra, 3, ba.Length);\n                return ra;\n            }\n            default:\n                return Encoding.ASCII.GetBytes(value);\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a array of string objects.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static byte[] GetBytes(this string[] value, Encoding enc = null)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (string[]) object cannot be null.\");\n        var numArray  = new byte[value.Where(ss => ss != null).Sum(ss => ss.Length)];\n        var dstOffset = 0;\n        foreach (var str in value)\n            if (str != null)\n            {\n                Buffer.BlockCopy(str.GetBytes(enc), 0, numArray, dstOffset, str.Length);\n                dstOffset += str.Length;\n            }\n        return numArray;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from an array of secure string objects\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    private static byte[] GetBytes(SecureString[] value)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (SecureString[]) object cannot be null.\");\n        var source = new List&lt;byte[]>();\n        foreach (var secureString in value)\n            if (secureString != null)\n            {\n                var byteArray = secureString.GetBytes();\n                source.Add(byteArray.CloneTo());\n            }\n        var seed = new byte[source.Sum(ba => ba.Length)];\n        return source.Aggregate(seed, (Current, ba) => Current.Add(ba));\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Get Bytes from a single secure string object using a specified encoding\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static unsafe byte[] GetBytes(this SecureString value, Encoding enc = null)\n    {\n        if (value == null)\n            throw new Exception(\"GetBytes (SecureString) object cannot be null.\");\n        if (enc == null)\n            enc = Encoding.Default;\n        var maxLength = enc.GetMaxByteCount(value.Length);\n        var bytes     = IntPtr.Zero;\n        var str       = IntPtr.Zero;\n        try\n        {\n            bytes = Marshal.AllocHGlobal(maxLength);\n            str   = Marshal.SecureStringToBSTR(value);\n            var chars  = (char*) str.ToPointer();\n            var bptr   = (byte*) bytes.ToPointer();\n            var len    = enc.GetBytes(chars, value.Length, bptr, maxLength);\n            var _bytes = new byte[len];\n            for (var i = 0; i &lt; len; ++i)\n            {\n                _bytes[i] = *bptr;\n                bptr++;\n            }\n            return _bytes;\n        }\n        finally\n        {\n            if (bytes != IntPtr.Zero)\n                Marshal.FreeHGlobal(bytes);\n            if (str != IntPtr.Zero)\n                Marshal.ZeroFreeBSTR(str);\n        }\n    }\n    private static byte[] GetBytes(xIntX[] value)\n    {\n        var r = Array.Empty&lt;byte>();\n        if (value != null)\n            for (var i = 0; i &lt; value.Length; ++i)\n            {\n                var lb = xIntX.GetBytesInt(value[i]);\n                Array.Resize(ref r, r.Length + lb.Length);\n                r = r.Add(lb);\n            }\n        return r;\n    }\n    private static byte[] GetBytes(BigInteger[] value)\n    {\n        var r = Array.Empty&lt;byte>();\n        if (value != null)\n            for (var i = 0; i &lt; value.Length; ++i)\n            {\n                var lb = GetBytes(value[i]);\n                Array.Resize(ref r, r.Length + lb.Length);\n                r = r.Add(lb);\n            }\n        return r;\n    }\n    private static byte[] GetBytes(BigInteger value)\n    {\n        return value.ToByteArray();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Gets list of byte arrays from a list of objects of type T.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static List&lt;byte[]> GetBytesObject&lt;T>(this List&lt;T> value)\n    {\n        return value.Select(o => o.GetBytes()).ToList();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Gets a single object of type T from a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static T ToObject&lt;T>(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"value cannot be null.\");\n        using (var stream = new MemoryStream(value))\n        {\n            var formatter = new BinaryFormatter();\n            var result    = (T) formatter.Deserialize(stream);\n            return result;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Gets an array of objects of type T from a list of byte arrays.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static T[] ToObject&lt;T>(this List&lt;byte[]> value)\n    {\n        if (value == null)\n            throw new Exception(\"value cannot be null.\");\n        if (value.Count == 0)\n            throw new Exception(\"value is empty.\");\n        var lst = new List&lt;T>();\n        foreach (var o in value)\n            lst.Add(o.ToObject&lt;T>());\n        return lst.ToArray();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Converts a hex string to a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static byte[] HexToBytes(this string hex)\n    {\n        if (hex.Length % 2 != 0)\n            throw new Exception($\"Incomplete Hex string {hex}\");\n        if (!hex.ContainsOnly(\"0123456789abcdefABCDEFxX\"))\n            throw new Exception(\"Error: hexNumber cannot contain characters other than 0-9,a-f,A-F, or xX\");\n        hex = hex.ToUpper();\n        if (hex.IndexOf(\"0X\", StringComparison.OrdinalIgnoreCase) != -1)\n            hex = hex.Substring(2);\n        return Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Converts a byte array to a hex string.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static string ToHexString(this byte[] bytes)\n    {\n        var sb = new StringBuilder();\n        foreach (var b in bytes)\n            sb.Append(b.ToString(\"X2\"));\n        return sb.ToString();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Converts a hex string to an unsigned long.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static unsafe ulong FromHexStringTo(this string value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        if (value.Length == 0)\n            return 0;\n        var ba = value.HexToBytes();\n        ba = ba.Invert();\n        if (ba.Length > 8)\n            throw new Exception(\"Maximum bit width is limited to 64 bits.\");\n        var len = ba.Length;\n        switch (len)\n        {\n            case 1:\n                fixed (byte* ptr = &amp;ba[0])\n                {\n                    return *ptr;\n                }\n            case 2:\n                fixed (byte* ptr = &amp;ba[0])\n                {\n                    return *ptr | ((ulong) ptr[1] &lt;&lt; 8);\n                }\n            case 3:\n                fixed (byte* ptr = &amp;ba[0])\n                {\n                    return *ptr | ((ulong) ptr[1] &lt;&lt; 8) | ((ulong) ptr[2] &lt;&lt; 16);\n                }\n            case 4:\n                fixed (byte* ptr = &amp;ba[0])\n                {\n                    return *ptr | ((ulong) ptr[1] &lt;&lt; 8) | ((ulong) ptr[2] &lt;&lt; 16) | ((ulong) ptr[3] &lt;&lt; 24);\n                }\n            case 5:\n                fixed (byte* ptr = &amp;ba[0])\n                {\n                    return *ptr | ((ulong) ptr[1] &lt;&lt; 8) | ((ulong) ptr[2] &lt;&lt; 16) | ((ulong) ptr[3] &lt;&lt; 24) | ((ulong) ptr[4] &lt;&lt; 32);\n                }\n            case 6:\n                fixed (byte* ptr = &amp;ba[0])\n                {\n                    return *ptr | ((ulong) ptr[1] &lt;&lt; 8) | ((ulong) ptr[2] &lt;&lt; 16) | ((ulong) ptr[3] &lt;&lt; 24) | ((ptr[4] | ((ulong) ptr[5] &lt;&lt; 8)) &lt;&lt; 32);\n                }\n            case 7:\n                fixed (byte* ptr = &amp;ba[0])\n                {\n                    return *ptr | ((ulong) ptr[1] &lt;&lt; 8) | ((ulong) ptr[2] &lt;&lt; 16) | ((ulong) ptr[3] &lt;&lt; 24) | ((ptr[4] | ((ulong) ptr[5] &lt;&lt; 8) | ((ulong) ptr[6] &lt;&lt; 16)) &lt;&lt; 32);\n                }\n            case 8:\n                fixed (byte* ptr = &amp;ba[0])\n                {\n                    return *ptr | ((ulong) ptr[1]                                                              &lt;&lt; 8) | ((ulong) ptr[2] &lt;&lt; 16) | ((ulong) ptr[3] &lt;&lt; 24) |\n                           ((ptr[4] | ((ulong) ptr[5] &lt;&lt; 8) | ((ulong) ptr[6] &lt;&lt; 16) | ((ulong) ptr[7] &lt;&lt; 24)) &lt;&lt; 32);\n                }\n            default:\n                return 0;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Converts a byte array to a hex string.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static string ToBinaryString(this byte[] bytes)\n    {\n        var len = bytes.Length;\n        var sb  = new StringBuilder();\n        for (var i = 0; i &lt; len; i++)\n            sb.Append(Convert.ToString(bytes[i], 2).PadLeft(8, '0'));\n        return sb.ToString();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Converts a binary string to an unsigned long.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static ulong FromBinaryStringTo(this string value)\n    {\n        var reversed = value.Reverse().ToArray();\n        var num      = 0ul;\n        for (var p = 0; p &lt; reversed.Count(); p++)\n        {\n            if (reversed[p] != '1')\n                continue;\n            num += (ulong) Math.Pow(2, p);\n        }\n        return num;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Converts a binary string to a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static byte[] GetBytesFromBinaryString(this string value)\n    {\n        if (!value.ContainsOnly(\"01\"))\n            throw new Exception($\"Error: Binary string can only contains 0's and 1's. Value:{value}\");\n        var len   = value.Length;\n        var bLen  = (int) Math.Ceiling(len \/ 8d);\n        var bytes = new byte[bLen];\n        var size  = 8;\n        for (var i = 1; i &lt;= bLen; i++)\n        {\n            var idx = len - 8 * i;\n            if (idx &lt; 0)\n            {\n                size = 8 + idx;\n                idx  = 0;\n            }\n            bytes[bLen - i] = Convert.ToByte(value.Substring(idx, size), 2);\n        }\n        return bytes;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Converts a byte array to a octal string.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static string ToOctalString(this byte[] value)\n    {\n        value = value.Invert();\n        var index = value.Length - 1;\n        var base8 = new StringBuilder();\n        var rem   = value.Length % 3;\n        if (rem == 0)\n            rem = 3;\n        var Base = 0;\n        while (rem != 0)\n        {\n            Base &lt;&lt;= 8;\n            Base +=  value[index--];\n            rem--;\n        }\n        base8.Append(Convert.ToString(Base, 8));\n        while (index >= 0)\n        {\n            Base = (value[index] &lt;&lt; 16) + (value[index - 1] &lt;&lt; 8) + value[index - 2];\n            base8.Append(Convert.ToString(Base, 8).PadLeft(8, '0'));\n            index -= 3;\n        }\n        return base8.ToString();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Boolean value converted from the byte at a specified position.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static bool ToBool(this byte[] value, int pos = 0)\n    {\n        return BitConverter.ToBoolean(value, pos);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Character value converted from the byte at a specified position.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static char ToChar(this byte[] value, int pos = 0)\n    {\n        return BitConverter.ToChar(value, pos);\n    }\n    public static unsafe byte ToByte(this byte[] value, int pos = 0)\n    {\n        byte bv;\n        fixed (byte* bp = value)\n        {\n            var bpp = bp + pos;\n            bv = *bpp;\n            return bv;\n        }\n    }\n    public static unsafe sbyte ToSByte(this byte[] value, int pos = 0)\n    {\n        fixed (byte* bp = value)\n        {\n            var ptr = bp + pos;\n            if (pos % 2 == 0)\n                return *(sbyte*) ptr;\n            return (sbyte) *ptr;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Short value converted from the byte at a specified position.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static short ToShort(this byte[] value, int pos = 0)\n    {\n        return BitConverter.ToInt16(PadShort(value), pos);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Unsigned Short value converted from the byte at a specified position.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static ushort ToUShort(this byte[] value, int pos = 0)\n    {\n        return BitConverter.ToUInt16(PadShort(value), pos);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Integer value converted from the byte at a specified position.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static int ToInt(this byte[] value, int pos = 0)\n    {\n        return BitConverter.ToInt32(PadInt(value), pos);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Unsigned Integer value converted from the byte at a specified position.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static uint ToUInt(this byte[] value, int pos = 0)\n    {\n        return BitConverter.ToUInt32(PadInt(value), pos);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Long value converted from the byte at a specified position.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static long ToLong(this byte[] value, int pos = 0)\n    {\n        return BitConverter.ToInt64(PadLong(value), pos);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Unsigned Long value converted from the byte at a specified position.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static ulong ToULong(this byte[] value, int pos = 0)\n    {\n        return BitConverter.ToUInt64(PadLong(value), pos);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a signed 128 Bit value.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static Int128 ToInt128(this byte[] value)\n    {\n        if (value.Length > 16)\n            throw new ArgumentException($\"Value length {value.Length} exceeds limit. {16}\");\n        return new Int128(value);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Unsigned 128 Bit value.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static UInt128 ToUInt128(this byte[] value)\n    {\n        if (value.Length > 16)\n            throw new ArgumentException($\"Value length {value.Length} exceeds limit. {16}\");\n        return new UInt128(value);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a signed 256 Bit value.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static Int256 ToInt256(this byte[] value)\n    {\n        if (value.Length > 32)\n            throw new ArgumentException($\"Value length {value.Length} exceeds limit. {32}\");\n        return new Int256(value);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Unsigned 256 Bit value.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static UInt256 ToUInt256(this byte[] value)\n    {\n        if (value.Length > 32)\n            throw new ArgumentException($\"Value length {value.Length} exceeds limit. {32}\");\n        return new UInt256(value);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a signed 512 Bit value.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static Int512 ToInt512(this byte[] value)\n    {\n        if (value.Length > 64)\n            throw new ArgumentException($\"Value length {value.Length} exceeds limit. {64}\");\n        return new Int512(value);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Unsigned 512 Bit value.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static UInt512 ToUInt512(this byte[] value)\n    {\n        if (value.Length > 64)\n            throw new ArgumentException($\"Value length {value.Length} exceeds limit. {64}\");\n        return new UInt512(value);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a signed 1024 Bit value.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static xIntX ToInt1024(this byte[] value)\n    {\n        if (value.Length > 128)\n            throw new ArgumentException($\"Value length {value.Length} exceeds limit. {128}\");\n        return new xIntX(value, 1024, false);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Unsigned 1024 Bit value.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static xIntX ToUInt1024(this byte[] value)\n    {\n        if (value.Length > 128)\n            throw new ArgumentException($\"Value length {value.Length} exceeds limit. {128}\");\n        return new xIntX(value, 1024, true);\n    }\n    public static xIntX ToxIntX(this byte[] value)\n    {\n        var bl = value.Length * 8;\n        return new xIntX(value, bl, false);\n    }\n    public static BigInteger ToBigInteger(this byte[] value)\n    {\n        return new(value);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Float value converted from the byte at a specified position.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static float ToFloat(this byte[] value, int pos = 0)\n    {\n        return BitConverter.ToSingle(PadInt(value), pos);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Double value converted from the byte at a specified position.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static double ToDouble(this byte[] value, int pos = 0)\n    {\n        return BitConverter.ToDouble(PadLong(value), pos);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Decimal value converted from the byte at a specified position.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static unsafe decimal ToDecimal(this byte[] value, int pos = 0)\n    {\n        decimal dv;\n        fixed (byte* bp = value)\n        {\n            var bpp = bp + pos;\n            dv = *(decimal*) bpp;\n        }\n        return dv;\n    }\n    public static decimal ToDecimalL(byte[] src, int offset)\n    {\n        var i1 = BitConverter.ToInt32(src, offset);\n        var i2 = BitConverter.ToInt32(src, offset + 4);\n        var i3 = BitConverter.ToInt32(src, offset + 8);\n        var i4 = BitConverter.ToInt32(src, offset + 12);\n        return new decimal(new[] {i1, i2, i3, i4});\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a String value converted from the byte at a specified position.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static string ToString(this byte[] value, int pos = 0)\n    {\n        return BitConverter.ToString(value, pos);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Secure String value converted from the byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static SecureString ToSecureString(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var securestring = new SecureString();\n        var asCharA      = value.ToCharArray();\n        foreach (var c in asCharA)\n            securestring.AppendChar(c);\n        return securestring;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Boolean array converted from a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static bool[] ToBooleanArray(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var arr = new bool[value.Length];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Character array converted from a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static char[] ToCharArray(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var arr = new char[value.Length];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    public static byte[] ToByteArray(this byte[] value, int index = 0, int length = -1)\n    {\n        if (length == -1)\n            length = value.Length - index;\n        var ba = new byte[length];\n        Buffer.BlockCopy(value, index, ba, 0, length);\n        return ba;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a SByte array converted from a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static sbyte[] ToSByteArray(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var arr = new sbyte[value.Length];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Short array converted from a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static short[] ToShortArray(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var arr = new short[value.Length \/ 2];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Unsigned Short array converted from a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static ushort[] ToUShortArray(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var arr = new ushort[value.Length \/ 2];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Integer array converted from a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static int[] ToIntArray(this byte[] value, bool pad = false)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        if (pad)\n            value = PadInt(value);\n        var arr = new int[value.Length \/ 4];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Unsigned Integer array converted from a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static uint[] ToUIntArray(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var arr = new uint[value.Length \/ 4];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Long array converted from the byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static long[] ToLongArray(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var arr = new long[value.Length \/ 8];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Unsigned Long array converted from a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static ulong[] ToULongArray(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var arr = new ulong[value.Length \/ 8];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Float array converted from a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static float[] ToFloatArray(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        if (value.Length % 4 != 0)\n            throw new Exception(\"Byte Object length must be a multiple of 4\");\n        var arr = new List&lt;float>();\n        for (var i = 0; i &lt; value.Length; i += 4)\n        {\n            var f = BitConverter.ToSingle(value.SubByte(i, 4), 0);\n            arr.Add(f);\n        }\n        return arr.ToArray();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Double array converted from a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static double[] ToDoubleArray(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        if (value.Length % 8 != 0)\n            throw new Exception(\"Byte Object length must be a multiple of 8\");\n        var arr = new List&lt;double>();\n        for (var i = 0; i &lt; value.Length; i += 8)\n        {\n            var d = BitConverter.ToDouble(value.SubByte(i, 8), 0);\n            arr.Add(d);\n        }\n        return arr.ToArray();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a decimal array converted from a byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static decimal[] ToDecimalArray(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        if (value.Length % 16 != 0)\n            throw new Exception(\"Byte Object length must be a multiple of 16\");\n        var arr = new List&lt;decimal>();\n        for (var i = 0; i &lt; value.Length; i += 16)\n        {\n            var t = new[]\n            {\n                value[i], value[i + 1], value[i  + 2], value[i  + 3], value[i + 4], value[i  + 5], value[i  + 6],\n                value[i           + 7], value[i  + 8], value[i  + 9], value[i + 10], value[i + 11], value[i + 12],\n                value[i           + 13], value[i + 14], value[i + 15]\n            };\n            arr.Add(t.ToDecimal());\n        }\n        return arr.ToArray();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Returns a Single String converted from the byte array.\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static string ToSingleString(this byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var enc = GetEncoding(value);\n        switch (enc)\n        {\n            case ASCIIEncoding AsciiEncoding:\n                return Encoding.ASCII.GetString(value);\n            case UnicodeEncoding UnicodeEncoding:\n                return Encoding.Unicode.GetString(value);\n            case UTF32Encoding Utf32Encoding:\n                return Encoding.UTF32.GetString(value);\n            case UTF7Encoding Utf7Encoding:\n                return Encoding.UTF7.GetString(value);\n            case UTF8Encoding Utf8Encoding:\n                return Encoding.UTF8.GetString(value);\n            default:\n                return Encoding.ASCII.GetString(value);\n        }\n    }\n    private static Encoding GetEncoding(byte[] Data)\n    {\n        if (Data == null)\n            throw new Exception(\"Array cannot be null.\");\n        if (Data.Length &lt; 2)\n            return Encoding.Default;\n        if (Data[0] == 0xff &amp;&amp; Data[1] == 0xfe)\n            return Encoding.Unicode;\n        if (Data[0] == 0xfe &amp;&amp; Data[1] == 0xff)\n            return Encoding.BigEndianUnicode;\n        if (Data.Length &lt; 3)\n            return Encoding.Default;\n        if (Data[0] == 0xef &amp;&amp; Data[1] == 0xbb &amp;&amp; Data[2] == 0xbf)\n            return Encoding.UTF8;\n        if (Data[0] == 0x2b &amp;&amp; Data[1] == 0x2f &amp;&amp; Data[2] == 0x76)\n            return Encoding.UTF7;\n        if (Data.Length &lt; 4)\n            return Encoding.Default;\n        if (Data[0] == 0xff &amp;&amp; Data[1] == 0xfe &amp;&amp; Data[2] == 0 &amp;&amp; Data[3] == 0)\n            return Encoding.UTF32;\n        return Encoding.Default;\n    }\n    public static bool ToBool(this string value)\n    {\n        bool result = default;\n        if (!string.IsNullOrEmpty(value))\n            bool.TryParse(value, out result);\n        return result;\n    }\n    public static char ToChar(this string value)\n    {\n        char result = default;\n        if (!string.IsNullOrEmpty(value))\n            char.TryParse(value, out result);\n        return result;\n    }\n    public static byte ToByte(this string value)\n    {\n        byte result = default;\n        if (!string.IsNullOrEmpty(value))\n            byte.TryParse(value, out result);\n        return result;\n    }\n    public static sbyte ToSByte(this string value)\n    {\n        sbyte result = default;\n        if (!string.IsNullOrEmpty(value))\n            sbyte.TryParse(value, out result);\n        return result;\n    }\n    public static short ToInt16(this string value)\n    {\n        short result = 0;\n        if (!string.IsNullOrEmpty(value))\n            short.TryParse(value, out result);\n        return result;\n    }\n    public static ushort ToUInt16(this string value)\n    {\n        ushort result = 0;\n        if (!string.IsNullOrEmpty(value))\n            ushort.TryParse(value, out result);\n        return result;\n    }\n    public static int ToInt32(this string value)\n    {\n        var result = 0;\n        if (!string.IsNullOrEmpty(value))\n            int.TryParse(value, out result);\n        return result;\n    }\n    public static uint ToUInt32(this string value)\n    {\n        uint result = 0;\n        if (!string.IsNullOrEmpty(value))\n            uint.TryParse(value, out result);\n        return result;\n    }\n    public static long ToInt64(this string value)\n    {\n        long result = 0;\n        if (!string.IsNullOrEmpty(value))\n            long.TryParse(value, out result);\n        return result;\n    }\n    public static ulong ToUInt64(this string value)\n    {\n        ulong result = 0;\n        if (!string.IsNullOrEmpty(value))\n            ulong.TryParse(value, out result);\n        return result;\n    }\n    public static float ToFloat(this string value)\n    {\n        float result = 0;\n        if (!string.IsNullOrEmpty(value))\n            float.TryParse(value, out result);\n        return result;\n    }\n    public static double ToDouble(this string value)\n    {\n        double result = 0;\n        if (!string.IsNullOrEmpty(value))\n            double.TryParse(value, out result);\n        return result;\n    }\n    public static decimal ToDecimal(this string value)\n    {\n        decimal result = 0;\n        if (!string.IsNullOrEmpty(value))\n            decimal.TryParse(value, out result);\n        return result;\n    }\n    public static bool ToBool(this char value)\n    {\n        bool result = default;\n        if (!string.IsNullOrEmpty(value.ToString()))\n            bool.TryParse(value.ToString(), out result);\n        return result;\n    }\n    public static byte ToByte(this char value)\n    {\n        byte result = default;\n        if (!string.IsNullOrEmpty(value.ToString()))\n            byte.TryParse(value.ToString(), out result);\n        return result;\n    }\n    public static sbyte ToSByte(this char value)\n    {\n        sbyte result = default;\n        if (!string.IsNullOrEmpty(value.ToString()))\n            sbyte.TryParse(value.ToString(), out result);\n        return result;\n    }\n    public static short ToInt16(this char value)\n    {\n        short result = 0;\n        if (!string.IsNullOrEmpty(value.ToString()))\n            short.TryParse(value.ToString(), out result);\n        return result;\n    }\n    public static ushort ToUInt16(this char value)\n    {\n        ushort result = 0;\n        if (!string.IsNullOrEmpty(value.ToString()))\n            ushort.TryParse(value.ToString(), out result);\n        return result;\n    }\n    public static int ToInt32(this char value)\n    {\n        var result = 0;\n        if (!string.IsNullOrEmpty(value.ToString()))\n            int.TryParse(value.ToString(), out result);\n        return result;\n    }\n    public static uint ToUInt32(this char value)\n    {\n        uint result = 0;\n        if (!string.IsNullOrEmpty(value.ToString()))\n            uint.TryParse(value.ToString(), out result);\n        return result;\n    }\n    public static long ToInt64(this char value)\n    {\n        long result = 0;\n        if (!string.IsNullOrEmpty(value.ToString()))\n            long.TryParse(value.ToString(), out result);\n        return result;\n    }\n    public static ulong ToUInt64(this char value)\n    {\n        ulong result = 0;\n        if (!string.IsNullOrEmpty(value.ToString()))\n            ulong.TryParse(value.ToString(), out result);\n        return result;\n    }\n    public static float ToFloat(this char value)\n    {\n        float result = 0;\n        if (!string.IsNullOrEmpty(value.ToString()))\n            float.TryParse(value.ToString(), out result);\n        return result;\n    }\n    public static double ToDouble(this char value)\n    {\n        double result = 0;\n        if (!string.IsNullOrEmpty(value.ToString()))\n            double.TryParse(value.ToString(), out result);\n        return result;\n    }\n    public static decimal ToDecimal(this char value)\n    {\n        decimal result = 0;\n        if (!string.IsNullOrEmpty(value.ToString()))\n            decimal.TryParse(value.ToString(), out result);\n        return result;\n    }\n    private static byte[] PadLong(byte[] ba)\n    {\n        var s = ba.Length % 8;\n        switch (s)\n        {\n            case 0:\n                break;\n            case 1:\n                Array.Resize(ref ba, ba.Length + 7);\n                ba[ba.Length - 1] = 0;\n                ba[ba.Length - 2] = 0;\n                ba[ba.Length - 3] = 0;\n                ba[ba.Length - 4] = 0;\n                ba[ba.Length - 5] = 0;\n                ba[ba.Length - 6] = 0;\n                ba[ba.Length - 7] = 0;\n                break;\n            case 2:\n                Array.Resize(ref ba, ba.Length + 6);\n                ba[ba.Length - 1] = 0;\n                ba[ba.Length - 2] = 0;\n                ba[ba.Length - 3] = 0;\n                ba[ba.Length - 4] = 0;\n                ba[ba.Length - 5] = 0;\n                ba[ba.Length - 6] = 0;\n                break;\n            case 3:\n                Array.Resize(ref ba, ba.Length + 5);\n                ba[ba.Length - 1] = 0;\n                ba[ba.Length - 2] = 0;\n                ba[ba.Length - 3] = 0;\n                ba[ba.Length - 4] = 0;\n                ba[ba.Length - 5] = 0;\n                break;\n            case 4:\n                Array.Resize(ref ba, ba.Length + 4);\n                ba[ba.Length - 1] = 0;\n                ba[ba.Length - 2] = 0;\n                ba[ba.Length - 3] = 0;\n                ba[ba.Length - 4] = 0;\n                break;\n            case 5:\n                Array.Resize(ref ba, ba.Length + 3);\n                ba[ba.Length - 1] = 0;\n                ba[ba.Length - 2] = 0;\n                ba[ba.Length - 3] = 0;\n                break;\n            case 6:\n                Array.Resize(ref ba, ba.Length + 2);\n                ba[ba.Length - 1] = 0;\n                ba[ba.Length - 2] = 0;\n                break;\n            case 7:\n                Array.Resize(ref ba, ba.Length + 1);\n                ba[ba.Length - 1] = 0;\n                break;\n        }\n        return ba;\n    }\n    private static byte[] PadInt(byte[] ba)\n    {\n        var s = ba.Length % 4;\n        switch (s)\n        {\n            case 0:\n                break;\n            case 1:\n                Array.Resize(ref ba, ba.Length + 3);\n                ba[ba.Length - 1] = 0;\n                ba[ba.Length - 2] = 0;\n                ba[ba.Length - 3] = 0;\n                break;\n            case 2:\n                Array.Resize(ref ba, ba.Length + 2);\n                ba[ba.Length - 1] = 0;\n                ba[ba.Length - 2] = 0;\n                break;\n            case 3:\n                Array.Resize(ref ba, ba.Length + 1);\n                ba[ba.Length - 1] = 0;\n                break;\n        }\n        return ba;\n    }\n    private static byte[] PadShort(byte[] ba)\n    {\n        var s = ba.Length % 2;\n        switch (s)\n        {\n            case 0:\n                break;\n            case 1:\n                Array.Resize(ref ba, ba.Length + 1);\n                ba[ba.Length - 1] = 0;\n                break;\n        }\n        return ba;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Raw byte array from string\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static byte[] GetBytesFromString(this string str)\n    {\n        if (str == null)\n            throw new ArgumentNullException(\"string cannot be null.\");\n        if (str.Length == 0)\n            return Array.Empty&lt;byte>();\n        var bytes = new byte[str.Length * sizeof(char)];\n        Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);\n        return bytes;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Raw string from byte array\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static string GetStringFromBytes(this byte[] bytes)\n    {\n        if (bytes == null)\n            throw new ArgumentNullException(\"bytes cannot be null.\");\n        if (bytes.Length % _charSize != 0)\n            throw new ArgumentException(\"Invalid bytes length\");\n        if (bytes.Length == 0)\n            return string.Empty;\n        var chars = new char[bytes.Length \/ sizeof(char)];\n        Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);\n        return new string(chars);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Takes a byte array and converts it to a non-serialized object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static T NonSerialByteArrayToObject&lt;T>(this byte[] data)\n    {\n        var target = (T) Activator.CreateInstance(typeof(T), null);\n        using (var ms = new MemoryStream(data))\n        {\n            byte[] ba    = null;\n            var    infos = typeof(T).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);\n            foreach (var info in infos)\n            {\n                ba = new byte[sizeof(int)];\n                ms.Read(ba, 0, sizeof(int));\n                var size = BitConverter.ToInt32(ba, 0);\n                ba = new byte[size];\n                ms.Read(ba, 0, size);\n                var bf = new BinaryFormatter();\n                using (var ms1 = new MemoryStream(ba))\n                {\n                    info.SetValue(target, bf.Deserialize(ms1));\n                }\n            }\n        }\n        return target;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Takes a non-serialized object and converts it into a byte array\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static byte[] SerializeObjectNonSerial&lt;T>(T obj)\n    {\n        using (var ms = new MemoryStream())\n        {\n            var infos = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);\n            foreach (var info in infos)\n            {\n                var bf = new BinaryFormatter();\n                using (var inMStream = new MemoryStream())\n                {\n                    var v = info.GetValue(obj);\n                    if (v != null)\n                    {\n                        bf.Serialize(inMStream, v);\n                        var ba = inMStream.ToArray();\n                        ms.Write(ba.Length.GetBytes(), 0, sizeof(int));\n                        ms.Write(ba,                   0, ba.Length);\n                    }\n                }\n            }\n            return ms.ToArray();\n        }\n    }\n    private static IEnumerable&lt;FieldInfo> GetAllFields(Type type)\n    {\n        return type.GetNestedTypes().SelectMany(GetAllFields).Concat(type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance));\n    }\n    [SecurityCritical]\n    private static byte[] GetUnClassifiedBytesArray(object obj)\n    {\n        if (obj == null)\n            throw new ArgumentException(\"Object cannot be null.\");\n        if (!obj.GetType().IsArray)\n            throw new ArgumentException(\"Object type needs to be of type array.\");\n        var aObj = ((IEnumerable) obj).Cast&lt;object>().ToArray();\n        var ba   = new List&lt;byte[]>();\n        for (var i = 0; i &lt; aObj.Length; ++i)\n            ba.Add(GetUnClassifiedBytes(aObj[i], aObj[i].GetType()));\n        var ba1 = ba.SelectMany(a => a).ToArray();\n        return ba1;\n    }\n    [SecurityCritical]\n    private static byte[] GetUnClassifiedBytes(object obj, Type type)\n    {\n        if (obj == null)\n            throw new ArgumentException(\"Object cannot be null.\");\n        var af = GetAllFields(type);\n        using (var ms = new RawMemoryStreamWriter())\n        {\n            foreach (var infon in af)\n            {\n                var v = infon.IsStatic ? infon.GetValue(null) : infon.GetValue(obj);\n                if (v != null)\n                    ms.Write(v);\n            }\n            if (ms.BaseStream.Length == 0)\n                throw new Exception(\"Value and Reference Types, no meaningful data can be found.\");\n            return ms.BaseStream.ToArray();\n        }\n    }\n    [SecurityCritical]\n    public static byte[] GetBytesReflection(this object obj)\n    {\n        var ltype = obj.GetType();\n        return GetUnClassifiedBytes(obj, ltype);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Serialize a serializable object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static byte[] SerializeObject(this object obj)\n    {\n        using (var stream = new MemoryStream())\n        {\n            Formatter.Serialize(stream, obj);\n            return stream.ToArray();\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     DeSerialize a serialized object\n    \/\/\/     (GetBytesClass.cs)\n    \/\/\/ &lt;\/summary>\n    public static T DeserializeObject&lt;T>(this byte[] bytes)\n    {\n        using (var stream = new MemoryStream(bytes))\n        {\n            var result = (T) Formatter.Deserialize(stream);\n            return result;\n        }\n    }\n    public static byte[] ObjectToByteArray(this object obj)\n    {\n        if (obj == null)\n            return null;\n        var bf = new BinaryFormatter();\n        var ms = new MemoryStream();\n        bf.Serialize(ms, obj);\n        return ms.ToArray();\n    }\n    public static object ByteArrayToObject(this byte[] arrBytes)\n    {\n        var memStream = new MemoryStream();\n        var binForm   = new BinaryFormatter();\n        memStream.Write(arrBytes, 0, arrBytes.Length);\n        memStream.Seek(0, SeekOrigin.Begin);\n        var obj = binForm.Deserialize(memStream);\n        return obj;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>GetBytes Arrays, Classes, Structures, Primitives Updated: Apr-15,2021<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[25,160],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/354"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=354"}],"version-history":[{"count":7,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/354\/revisions"}],"predecessor-version":[{"id":404,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/354\/revisions\/404"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}