• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2629
  • Last Modified:

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

0
korpiklaani
Asked:
korpiklaani
  • 4
  • 4
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now