ByteArraySegment.cs

Posted on June 17, 2020June 17, 2020Tags , , ,   Leave a comment on ByteArraySegment.cs

Segment a byte array to 16, 32, 64 bit Arrays

using System;
using System.Collections.Generic;
using System.Numerics;
public class ByteArraySegment
{
    public IEnumerable<T[]> SegmentEnum<T>(T[] buffer, int size)
    {
        var idx = 0;
        var ptr = buffer.Length;
        while (ptr >= size)
        {
            var array = new T[size];
            Buffer.BlockCopy(buffer, idx, array, 0, size);
            ptr -= size;
            idx += size;
            yield return array;
        }
    }
    public List<byte[]> SegmentList(byte[] buffer, int size)
    {
        var list = new List<byte[]>(buffer.Length >> 2);
        var idx  = 0;
        var ptr  = buffer.Length;
        while (ptr >= size)
        {
            var _result = new byte[size];
            Buffer.BlockCopy(buffer, idx, _result, 0, size);
            list.Add(_result);
            ptr -= size;
            idx += size;
        }
        return list;
    }
    public char[] SegmentChar(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 2 != 0)
            throw new Exception("Buffer must be a multiple of 2.");
        var array = new char[len >> 1];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public short[] Segment16(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 2 != 0)
            throw new Exception("Buffer must be a multiple of 2.");
        var array = new short[len >> 1];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public ushort[] SegmentU16(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 2 != 0)
            throw new Exception("Buffer must be a multiple of 2.");
        var array = new ushort[len >> 1];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public int[] Segment32(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 4 != 0)
            throw new Exception("Buffer must be a multiple of 4.");
        var array = new int[len >> 2];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public float[] SegmentFloat(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 4 != 0)
            throw new Exception("Buffer must be a multiple of 4.");
        var array = new float[len >> 2];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public uint[] SegmentU32(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 4 != 0)
            throw new Exception("Buffer must be a multiple of 4.");
        var array = new uint[len >> 2];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public long[] Segment64(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 8 != 0)
            throw new Exception("Buffer must be a multiple of 8.");
        var array = new long[len >> 3];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public double[] SegmentDouble(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 8 != 0)
            throw new Exception("Buffer must be a multiple of 8.");
        var array = new double[len >> 3];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public ulong[] SegmentU64(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 8 != 0)
            throw new Exception("Buffer must be a multiple of 8.");
        var array = new ulong[len >> 3];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public BigInteger[] SegmentBI(byte[] buffer, int size)
    {
        var idx  = 0;
        var ptr  = buffer.Length;
        var bi   = new BigInteger[buffer.Length / size];
        var ptr1 = 0;
        while (ptr >= size)
        {
            var array = new byte[size];
            Buffer.BlockCopy(buffer, idx, array, 0, size);
            ptr      -= size;
            idx      += size;
            bi[ptr1++] =  new BigInteger(array);
        }
        return bi;
    }
}