{"id":88,"date":"2020-06-17T08:44:48","date_gmt":"2020-06-17T08:44:48","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=88"},"modified":"2020-06-17T10:02:30","modified_gmt":"2020-06-17T10:02:30","slug":"bytearraysegment-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/06\/17\/bytearraysegment-cs\/","title":{"rendered":"ByteArraySegment.cs"},"content":{"rendered":"\n<p>Segment a byte array to 16, 32, 64 bit Arrays<\/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.Generic;\nusing System.Numerics;\npublic class ByteArraySegment\n{\n    public IEnumerable&lt;T[]> SegmentEnum&lt;T>(T[] buffer, int size)\n    {\n        var idx = 0;\n        var ptr = buffer.Length;\n        while (ptr >= size)\n        {\n            var array = new T[size];\n            Buffer.BlockCopy(buffer, idx, array, 0, size);\n            ptr -= size;\n            idx += size;\n            yield return array;\n        }\n    }\n    public List&lt;byte[]> SegmentList(byte[] buffer, int size)\n    {\n        var list = new List&lt;byte[]>(buffer.Length >> 2);\n        var idx  = 0;\n        var ptr  = buffer.Length;\n        while (ptr >= size)\n        {\n            var _result = new byte[size];\n            Buffer.BlockCopy(buffer, idx, _result, 0, size);\n            list.Add(_result);\n            ptr -= size;\n            idx += size;\n        }\n        return list;\n    }\n    public char[] SegmentChar(byte[] buffer)\n    {\n        var len = buffer.Length;\n        if (len % 2 != 0)\n            throw new Exception(\"Buffer must be a multiple of 2.\");\n        var array = new char[len >> 1];\n        Buffer.BlockCopy(buffer, 0, array, 0, len);\n        return array;\n    }\n    public short[] Segment16(byte[] buffer)\n    {\n        var len = buffer.Length;\n        if (len % 2 != 0)\n            throw new Exception(\"Buffer must be a multiple of 2.\");\n        var array = new short[len >> 1];\n        Buffer.BlockCopy(buffer, 0, array, 0, len);\n        return array;\n    }\n    public ushort[] SegmentU16(byte[] buffer)\n    {\n        var len = buffer.Length;\n        if (len % 2 != 0)\n            throw new Exception(\"Buffer must be a multiple of 2.\");\n        var array = new ushort[len >> 1];\n        Buffer.BlockCopy(buffer, 0, array, 0, len);\n        return array;\n    }\n    public int[] Segment32(byte[] buffer)\n    {\n        var len = buffer.Length;\n        if (len % 4 != 0)\n            throw new Exception(\"Buffer must be a multiple of 4.\");\n        var array = new int[len >> 2];\n        Buffer.BlockCopy(buffer, 0, array, 0, len);\n        return array;\n    }\n    public float[] SegmentFloat(byte[] buffer)\n    {\n        var len = buffer.Length;\n        if (len % 4 != 0)\n            throw new Exception(\"Buffer must be a multiple of 4.\");\n        var array = new float[len >> 2];\n        Buffer.BlockCopy(buffer, 0, array, 0, len);\n        return array;\n    }\n    public uint[] SegmentU32(byte[] buffer)\n    {\n        var len = buffer.Length;\n        if (len % 4 != 0)\n            throw new Exception(\"Buffer must be a multiple of 4.\");\n        var array = new uint[len >> 2];\n        Buffer.BlockCopy(buffer, 0, array, 0, len);\n        return array;\n    }\n    public long[] Segment64(byte[] buffer)\n    {\n        var len = buffer.Length;\n        if (len % 8 != 0)\n            throw new Exception(\"Buffer must be a multiple of 8.\");\n        var array = new long[len >> 3];\n        Buffer.BlockCopy(buffer, 0, array, 0, len);\n        return array;\n    }\n    public double[] SegmentDouble(byte[] buffer)\n    {\n        var len = buffer.Length;\n        if (len % 8 != 0)\n            throw new Exception(\"Buffer must be a multiple of 8.\");\n        var array = new double[len >> 3];\n        Buffer.BlockCopy(buffer, 0, array, 0, len);\n        return array;\n    }\n    public ulong[] SegmentU64(byte[] buffer)\n    {\n        var len = buffer.Length;\n        if (len % 8 != 0)\n            throw new Exception(\"Buffer must be a multiple of 8.\");\n        var array = new ulong[len >> 3];\n        Buffer.BlockCopy(buffer, 0, array, 0, len);\n        return array;\n    }\n    public BigInteger[] SegmentBI(byte[] buffer, int size)\n    {\n        var idx  = 0;\n        var ptr  = buffer.Length;\n        var bi   = new BigInteger[buffer.Length \/ size];\n        var ptr1 = 0;\n        while (ptr >= size)\n        {\n            var array = new byte[size];\n            Buffer.BlockCopy(buffer, idx, array, 0, size);\n            ptr      -= size;\n            idx      += size;\n            bi[ptr1++] =  new BigInteger(array);\n        }\n        return bi;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Segment a byte array to 16, 32, 64 bit Arrays<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[35,34,5,33],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/88"}],"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=88"}],"version-history":[{"count":2,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":90,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions\/90"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}