Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I have to split bytes based on 0x1E
byte[] input = { 0x1E, 0x30, 0x30, 0x31, 0x1E };
how to split byte array?
Posted 27-Nov-12 19:50pm
Comments
Mohd. Mukhtar at 28-Nov-12 1:03am
   
What have you tried?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
Try with below line of code.
 
byte[]    input = { 0x1E, 0x30, 0x30, 0x31, 0x1E };
List<string> output = new List<string>();
 
int lastIndex = 0;
 
for (int i = 0; i < input.Length; i++) 
{
    if (input[i] == ',')
    {
         if (i - lastIndex == 4)
         {
             byte[] tmp = { input[i - 4], input[i - 3], input[i - 2], input[i - 1] };
             output.Add(BitConverter.ToInt32(tmp, 0));
         }
         lastIndex = i + 1;
    }
}
</string></string>
Refer the below link.
 
http://stackoverflow.com/questions/11816295/splitting-a-byte-into-multiple-byte-arrays-in-c-sharp[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
try this,
 
 
 static void Main()
        {
            byte[] arr = { 0x1E, 0x23, 0x1E, 0x33, 0x44, 0x1E };
            byte split = 0x1E;
            List<byte[]> result = new List<byte[]>();
            int start = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == split && i!=0)
                {
                    byte[] _in = new byte[i - start];
                    Array.Copy(arr, start, _in, 0, i - start);
                    result.Add(_in);
                    start = i+1;
                }
                else if (arr[i] == split && i == 0)
                {
                    start = i + 1;
                }
                else if (arr.Length - 1 == i && i != start)
                {
                    byte[] _in = new byte[i - start+1];
                    Array.Copy(arr, start, _in, 0, i - start+1);
                    result.Add(_in);
                }
 
            }
}

--SRJ

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Thank u all for reply i have acheived this thru yield keyword below is a sample
class Program
    {
        static IEnumerable<byte[]> Packetize(IEnumerable<byte> stream)
        {
            var buffer = new List<byte>();
            foreach (byte b in stream)
            {
                buffer.Add(b);
                if (b == 0x1E||b==0x1F||b==0x07)
                {
                    buffer.Remove(b);
                    yield return buffer.ToArray();
                    buffer.Clear();
                }
            }
            if (buffer.Count > 0)
                yield return buffer.ToArray();
        }
 
        static void Main(string[] args)
        {
            byte[] input = {0x1E, 0x30, 0x30, 0x31, 0x1E, 0x40, 0x41, 0x42, 0x1E, 0x1F };
            foreach (var packet in Packetize(input))
            {
                foreach (var b in packet) Console.Write("{0:x2} ", b);
                Console.WriteLine();
            }
        }
    }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.150326.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100