Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing byte array that contains different types of data

I have a fixed-length byte array that is 1250 bytes long. It may contain the following types of data:

  • Object A which consists of 5 bytes. The first byte contains the letter "A" and the next four bytes store an integer from 1 - 100000.

  • Object B which consists of 2 bytes. The first byte contains the letter "B" and the next byte contains an integer from 1 - 100.

  • Object C which consists of 50 bytes. All 50 bytes are used to store an ASCII-encoded string which will only consist of numbers and the following characters: - + ( and )

I don't know how many of each object type are in the byte array but I do know that they are grouped together (Object B, Object B, Object A, Object A, Object A, Object C, etc.). Most of the time when I parse a byte array, the array contains data of one type (all items are Object A, for example) so I know exactly how many bytes each item is comprised of and I just loop through the array processing the bytes. In this case, I have three different types of data that are all different lengths. I was thinking that I would need to do something like this:

int offset = 0;
while (offset <= 1250)
{
    string objectHeader = Encoding.ASCII.GetString(byteArray, offset, 1);

    if (objectHeader.Equals("A"))
    {
        // read 4 more bytes and then convert into int value (1 - 100000)
        index += 5;
    }
    else if (objectHeader.Equals("B"))
    {
        // read 1 more byte and then convert into int value (1 - 100)
        index += 2;
    }
    else
    {
        // read 49 more bytes and then convert into a string
        index += 50;
    }
}

Is there a better way of doing this?

like image 334
bmt22033 Avatar asked Feb 05 '13 19:02

bmt22033


People also ask

What is byte array data type?

The bytearray() method returns a bytearray object, which is an array of bytes. It returns a mutable series of integers between 0 and 256. The source parameter of the ByteArray is used to initialize the array.

What does a byte array contain?

A byte array is simply an area of memory containing a group of contiguous (side by side) bytes, such that it makes sense to talk about them in order: the first byte, the second byte etc..

Can byte array be stored in a string?

String also has a constructor where we can provide byte array and Charset as an argument. So below code can also be used to convert byte array to String in Java. String str = new String(byteArray, StandardCharsets. UTF_8);

Is binary same as byte array?

A byte is 8 bits of binary data so do byte array is an array of bytes used to store the collection of binary data.


2 Answers

Well, there seems to be a little confusion with offset and index, maybe you should be using a for-loop:

for(int index = 0; index < 1250; index++)
{
    switch(byteArray[index])
    {
         case (byte)'A':
             index++;
             int value = BitConverter.ToInt32(byteArray, index);
             index += 4;
             break;

       case (byte)'B':
             index++;
             // Read the next byte as integer.
             int value = (int)byteArray[index];
             index++;
             break;

       case (byte)'C':  // string.
             index++;
             // Read the next 49 bytes as an string.
             StringBuilder value = new StringBuilder(49);
             for(int i = index; i < index + 49; index++)
             {
                 if (byteArray[i] == 0) break;
                 value.Append(Converter.ToChar(byteArray[i]));
             }
             index+= 49;
             break;

       case 0:  // Finished.
             index = 1250;
             break;
       default:
             throw new InvalidArgumentException("Invalid byte array format");
    }
}

How do you see if there is no more objects? In my example I suggest it ends with a '\0'.

Good luck with your quest.

like image 68
Casperah Avatar answered Oct 06 '22 20:10

Casperah


      int offset = 0;
      while (offset <= 1250)
      {

        switch (byteArray[offset])
        {
          case (byte)'A':
            //read other data ..
            offset += 5;
            break;
          case (byte)'B':
            //read other data ..
            offset += 2;
            break;
          case (byte)'C':
            //read other data ..
            offset += 50;
            break;
          default:
            //error
            break;
        }
      } 

Or another variant with binary reader:

      var reader = new BinaryReader(new MemoryStream(byteArray), Encoding.ASCII);
      while (reader.BaseStream.Position < reader.BaseStream.Length)
      {
        switch(reader.ReadChar())
        {
          case 'A':
            {
              var i = reader.ReadInt32();
              return new TypeA(i);
            }
            break;
          case 'B':
            {
              var i = reader.ReadByte();
              return new TypeB(i);
            }
            break;
          case 'C':
            {
              var chars = reader.ReadChars(49);
              return new TypeC(new string(chars.TakeWhile(ch => ch != 0).ToArray()));
            }
            break;
        }

      }
like image 37
Serj-Tm Avatar answered Oct 06 '22 21:10

Serj-Tm