Skip to content
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

Array with non 8bit alignment #244

Open
xvaara opened this issue Aug 29, 2023 · 1 comment
Open

Array with non 8bit alignment #244

xvaara opened this issue Aug 29, 2023 · 1 comment

Comments

@xvaara
Copy link

xvaara commented Aug 29, 2023

Is it possible to have an array that isn't aligned to 8bits?

https://stackblitz.com/edit/stackblitz-starters-cxezrc?file=index.js

@nick-hunter
Copy link
Contributor

I don't think this is currently possible with an array. I would recommend using the getCode() method to understand why. For your example, each loop iteration is reading in 24 bits. $tmp2 = (dataView.getUint16(offset) << 8) | dataView.getUint8(offset + 2);. Any leftover bits will be dropped.

There is a hacky way to accomplish this, but you can't use the Array type. This may or may not make sense for your application. I would make sure the array as a whole is a multiple of 8 bits. Also be careful with nested bit fields. There are some issues if you have a nest inside a nest.

// 22    22    22...
// 10110 10110 10110 10110 10110 10110 00
const test = Buffer.from("B5AD6B58", "hex");

const p = new Parser();
for(let i = 0; i < 3; i++){
    p['bit5'](`a${i}`);
    p['bit5'](`b${i}`);
}

console.log(p.parse(test)) // { a0: 22, b0: 22, a1: 22, b1: 22, a2: 22, b2: 22 }

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants