raydac/java-binary-block-parser

Parse Bit Fields

wjfrelo opened this issue · 3 comments

Good day,

I am a intermediate level programmer and I have been unable to exactly understand how to use some of the methods within the code base. Partly because of my ignorance to bitwise operations.

Specifically, I am trying to parse a byte block that is 1024 bytes. Essentially, all though not exclusively, I am trying to parse some number of bits for a 4 byte block (32 bits). In some blocks it may be 15 bits, while in another it might be 7 bits.

How can I use the "script" or some other method to accomplish my goal. I just don't quite. understand the notations.

Any help would be really appreciated.

Thanks,

Wyatt

I am not sure that correctly understand the problem
for example the code below reads 4 byte blocks from array and extract 15 bit from some of them and 7 bit from another

    JBBPParser parser7bit = JBBPParser.prepare("bit:7 first;");
    JBBPParser parser15bit = JBBPParser.prepare("byte first; bit:7 second;");
    
    final byte [] testArray = new byte[1024];
    new Random(1234).nextBytes(testArray);
    
    final JBBPBitInputStream stream = new JBBPBitInputStream(new ByteArrayInputStream(testArray));
    
    final byte [] buffer = new byte[4];

    boolean parseAs7bit = true;
    
    while(stream.hasAvailableData()){
      stream.read(buffer);
      if (parseAs7bit) {
        final int value = parser7bit.parse(buffer).findFieldForType(JBBPFieldBit.class).getAsInt();
        System.out.println("Value 7 bit : "+value);
      } else {
        final JBBPFieldStruct parsed = parser15bit.parse(buffer);
        final int first = parsed.findFieldForPathAndType("first", JBBPFieldByte.class).getAsInt() & 0xFF;
        final int second = parsed.findFieldForPathAndType("second", JBBPFieldBit.class).getAsInt();
        System.out.println("Value 15 bit : "+((first<<7)|(second)));
      }
      
      parseAs7bit = !parseAs7bit;
    }

Thanks for the code. I definitely understand a lot better what and how to accomplish my goal. Have one more question.

In the following code that you placed above:
JBBPParser parser15bit = JBBPParser.prepare("byte first; bit:7 second;");
This makes sense as 15bits. Meaning, prepare to parse the first byte (8-bits) + the second 7-bits.

But, what if I wanted to parse the first 17bits and then the following 15bits. How do I say a range of bytes/bits, etc...? For example I would like to prepare to parse bytes 1 - 9 and bits 1 -4 from byte 10 together. Hopefully this makes sense.

I guess what I am asking is this, what arguments are being passed in this case ("byte first; bit:7 second;") and what do they do? I would imagine that this single string would be split on the ";" character. Then, they would be used to do something. I just kind find for what or identify the acceptable values. Having this information would be helpful.

Thanks, this library is very helpful.

Wyatt

java represents all streams as sequence of bytes and JBBP just load next byte into inside buffer and reads neede number of bits and LSB or MSB mode can be defined among JBBPInputStream parameters
in case "byte first; bit:7 second;" as the first one 8 bits will be read from stream and then 7 bits
sometime there can be very strange bit structures which can't be processed by JBBP out of the box, such structures should be processed manually with new custom data type