0

I have a byte array and want to split this with * char I am C++/Qt developer i can do this easy with this code in Qt

QByteArray byteArray;
QList<QByteArray> byteArrayList;

byteArray = file.readAll();
file.close();

byteArrayList = byteArray.split('*');

How can i split a byte array with char in C# ?

10
  • 5
    and what is a QByteArray? what API does that expose? that's not an inbuilt type, so... what happens with your code? does byterArray.split((byte)'*'); work? Commented Nov 21, 2017 at 15:21
  • this is C++Qt code Commented Nov 21, 2017 at 15:24
  • 1
    Because the op wants to know how to do it in c# Commented Nov 21, 2017 at 15:26
  • 1
    @TobiasTheel then - some reference to what they currently have in C# would be really useful; are they using byte[] byteArray = ... ? Commented Nov 21, 2017 at 15:27
  • 1
    Some additional information would truly be useful. Btw: doc.qt.io/qt-4.8/qbytearray.html Commented Nov 21, 2017 at 15:28

2 Answers 2

1
byte[] bytes = File.ReadAllBytes("yourtextfile.txt");
string[] x = Encoding.UTF8.GetString(bytes).Split('*');

Change encoding if needed.

Sign up to request clarification or add additional context in comments.

3 Comments

from comments: "this not text file"
You look expert in this field, Marc: stackoverflow.com/questions/11816295/…
binary network clients and binary serialization formats are two areas that I've certainly spent a lot of time in....
1

I'm not sure anything is inbuilt for that, since it isn't a common scenario; however you can search by index:

(all use of string here is purely for illustration; the actual split code doesn't use that)

static void Main()
{
    // pretend this isn't text
    byte[] bytes = Encoding.ASCII.GetBytes("askdjhkas*hdaskjdhakjshdjkahs*dkujyash");

    foreach(var chunk in Split(bytes, (byte)'*'))
    {
        // cheating with text to see if it worked
        var s = Encoding.ASCII.GetString(chunk.Array, chunk.Offset, chunk.Count);
        Console.WriteLine(s);
    }
}

static IEnumerable<ArraySegment<byte>> Split(byte[] data, byte splitBy)
{
    int start = 0, end;
    while((end = Array.IndexOf<byte>(data, splitBy, start)) > 0)
    {

        yield return new ArraySegment<byte>(data, start, end - start);
        start = end + 1;
    }
    end = data.Length;
    if ((end - start) > 0)
    {
        yield return new ArraySegment<byte>(data, start, end - start);
    }
}

Note: this would be a great scenario for "span" when that lands.

5 Comments

Can you please edit the question so it is clear what supposed to happen? (Is OP looking for equivalent of string.Split but for arrays?)
@AlexeiLevenkov I think so, yes
how to add this bytes to a list of bytes List<byte[]> bytesList = new List<byte[]>();
@ARASHz4 I would strongly advise against that - that is going to force you to allocate more memory. Could you live with List<ArraySegment<byte>>> ? if so: var bytesList = Split(bytes, (byte)'*').ToList();. As I say, "span" would be idea for this and would avoid extra allocs. If you must duplicate all the pieces, it can be done, sure
@marc-gravell how to convert List <ArraySegment<byte> > to List <byte[]>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.