Split byte array

I have to split a byte array on specific bytes.
For example i have a byte array with the bytes 123 456789 123abcdef
i want to split everytime 123 occurs and then save it translated into a string array but i cant find out howto =/ can anyone help me?

thanks

byte[] notsplitted = {0xAB, 0xFE, 0x10, 0x12, 0x13, 0xAB, 0xFE};

byte[] split_at = {0x10, 0x12, 0x13};

//after split it should look like this: 0xAB 0xFE 0xAB 0xFE
//and translated into a string array

Open in new window

korpiklaaniAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WesWilsonCommented:
I need some clarification.

1. In the example you show, do you want to split at the full sequence (0x10, 0x12, 0x13) or when any of those elements occur?

2. Ultimately, you are looking for multiple strings, split by the specified characters, but not containing those specified characters, correct?
0
korpiklaaniAuthor Commented:
i want to split the array "notsplitted" its like the String.Split() function in visual studio c#.
I just want to use it with a byte array, i want to split a byte array into several pieces everytime "0x10, 0x12, 0x13" occours. For example: ABC DEF ABC when DEF is the delimiter it should look like ABC ABC at the end. Then just translate ABC into a string array (Encoding.ASCII.GetString()) and give back a string array. But thats not that much important right now, the most important part is just to split the bytes because i cant get it working ):

thanks
0
WesWilsonCommented:
So the end result should be a single string, right? We are just removing the byte sequence in split_at from the original array?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

korpiklaaniAuthor Commented:
a string array because there are several byte arrays after splitting
0
WesWilsonCommented:
Let me know if this works for you.

byte[] notsplitted = {0xAB, 0xFE, 0x10, 0x12, 0x13, 0xAB, 0xFE};

byte[] split_at = {0x10, 0x12, 0x13};

List<string> splitStrings = new List<string>();
StringBuilder builder = new StringBuilder();
for(int i = 0; i < notsplitted.Length; ++i)
{
  if(notsplitted[i] == split_at[0] && i + split_At.Length <= notsplitted.Length)
  {
    for(int splitterIndex = 0; splitterIndex < split_at.Length; ++i)
    {
      if(notsplitted[i + splitterIndex] != split_at[splitterIndex])
      {
        break; //split sequence not found
      }
    }
    //split sequence matched. Do the split
    splitStrings.Add(builder.ToString());
    builder = new StringBuilder();
    i += (split_At.Length - 1);
    continue;
  }
  builder.Append(notsplitted[i]);
}
if(builder.Length > 0)
{
  splitStrings.Add(builder.ToString());
}
//splitStrings is a List<string>, but if you want it in array form, call ToArray()
string[] stringArray = splitStrings.ToArray();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
korpiklaaniAuthor Commented:
it does work i guess but it adds the bytes into a string without translation?
i need it to get translated like 0x61 is 'a'
0
WesWilsonCommented:
What if we cast the byte to char? Change line 24 to:

builder.Append((char)notsplitted[i]);

Open in new window

0
korpiklaaniAuthor Commented:
thanks! you are the best!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.