I am looking a way by C# to split an array into multiple sub-arrays that starts with specific value.
I have an array like this:
byte[] bytes = new byte[9];
bytes[0]=0x1a;
bytes[1]=0xAA;
bytes[2]=0xab;
bytes[3]=0xad;
bytes[4]=0xAA;
bytes[5]=0x01;
bytes[6]=0x02;
bytes[7]=0xAA;
bytes[8]= 0xac;
i want to split it into multiply arrays so that every new array start with 0xAA like below:
Array1: 0xAA, 0xab, 0xad
Array2: 0xAA, 0x01 0x02
Array3: 0xAA, 0xac
But i do not know how to implement.
Please help me some hints or codes. Thanks
You can iterate over the array with a for loop.
This finds occurences of 0xAA and creates the subarrays as copies. If you don't need a copy you may also create spans of the array regions like so:
If you assign something to an element of span it will change your original array. If you don't want this you can use
ReadOnlySpan<byte>in exactly the same way. But even then if you change the original array's values the span will reflect those changes.