Giter Site home page Giter Site logo

Parse Bit Fields about java-binary-block-parser HOT 3 CLOSED

raydac avatar raydac commented on September 13, 2024
Parse Bit Fields

from java-binary-block-parser.

Comments (3)

raydac avatar raydac commented on September 13, 2024

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

from java-binary-block-parser.

wjfrelo avatar wjfrelo commented on September 13, 2024

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

from java-binary-block-parser.

raydac avatar raydac commented on September 13, 2024

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

from java-binary-block-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.