-
Notifications
You must be signed in to change notification settings - Fork 33
How to parse bits #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
by default JBBP reads from zero bit, and in your case you should read from 7th bit and reverse values byte [] data = new byte[]{0b00000001, (byte)0b10100001, 0b00000000};
JBBPBitInputStream in = new JBBPBitInputStream(new ByteArrayInputStream(data), JBBPBitOrder.MSB0);
assertEquals(1, JBBPFieldByte.reverseBits((byte)in.read()));
assertEquals(5, JBBPFieldBit.reverseBits((byte)in.readBits(JBBPBitNumber.BITS_3), JBBPBitNumber.BITS_3));
assertEquals(8, JBBPFieldByte.reverseBits((byte)in.read()));
JBBPParser parser = JBBPParser.prepare("byte a; bit:3 b; byte c;", JBBPBitOrder.MSB0);
JBBPFieldStruct struct = parser.parse(data);
assertEquals(1, struct.findFieldForNameAndType("a", JBBPFieldByte.class).getAsInvertedBitOrder());
assertEquals(5, struct.findFieldForNameAndType("b", JBBPFieldBit.class).getAsInvertedBitOrder());
assertEquals(8, struct.findFieldForNameAndType("c", JBBPFieldByte.class).getAsInvertedBitOrder()); |
Thank you for the quick response. I am not that familiar with Big vs Little Endian but shouldn't this property solve this issue? Also, I kind of struggle to understand the reasoning why JBBP starts on the last bit instead of the first bit when extracting n bits of the byte stream. For me it is not clear, why this library is "jumping around" between the bytes in the array. IIUC JBBP seems to split this example like
|
at present it works in such way final byte[] data = new byte[] {0b0000_0001};
JBBPBitInputStream in = new JBBPBitInputStream(new ByteArrayInputStream(data));
assertEquals(0b0001, in.readBits(BITS_4));
assertEquals(0b0000, in.readBits(BITS_4));
assertEquals(0b0000_0001, new JBBPBitInputStream(new ByteArrayInputStream(data)).read());
in = new JBBPBitInputStream(new ByteArrayInputStream(data), JBBPBitOrder.LSB0);
assertEquals(0b0001, in.readBits(BITS_4));
assertEquals(0b0000, in.readBits(BITS_4));
assertEquals(0b0000_0001,
new JBBPBitInputStream(new ByteArrayInputStream(data), JBBPBitOrder.LSB0).read());
in = new JBBPBitInputStream(new ByteArrayInputStream(data), JBBPBitOrder.MSB0);
assertEquals(0b0000, in.readBits(BITS_4));
assertEquals(0b1000, in.readBits(BITS_4));
assertEquals(0b1000_0000,
new JBBPBitInputStream(new ByteArrayInputStream(data), JBBPBitOrder.MSB0).read()); so we get reversed data for MSB0 because it depends on filling of internal bit buffer during read, but looks that it is good idea to provide way to read and non reversed data, I will investigate how to add mode without reverse |
I have added MSB0_DIRECT mode which doesn't make reverse during read and write, you can try 3.0.1-SNAPSHOT |
Awesome, thank you very much! I tested it and it works perfectly fine for my use case :) |
Hey,
I played around with you library and wanted to figure out how I can parse a sequence like one byte, then 3 bits and then one byte with Big Endian mode.
I created the following JBBP script:
which generated
To test this, I used this data:
0x01a100
I want to get
but get
I used this JUnit Test:
Can you help me to use your library to reach my expected outcome? Thank you!
The text was updated successfully, but these errors were encountered: