Split Byte array by length - CSharp System

CSharp examples for System:Byte Array

Description

Split Byte array by length

Demo Code


using System.Collections.Generic;
using System;//  w w  w .ja v  a  2s.c  o  m

public class Main{
        private static List<byte[]> SplitBytes(byte[] bytes, int length)
        {
            List<byte[]> result = new List<byte[]>();

            int position = 0;
            while (bytes.Length - position > length)
            {
                byte[] temp = new byte[length];
                for (int i = 0; i < temp.Length; i++) temp[i] = bytes[i + position];
                position += length;
                result.Add(temp);
            }
            if (position < bytes.Length)
            {
                byte[] temp = new byte[bytes.Length - position];
                for (int i = 0; i + position < bytes.Length; i++) temp[i] = bytes[i + position];
                result.Add(temp);
            }
            return result;
        }
}

Related Tutorials