-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
As we can construct a bitset object by a Buffer, Can we convert the bitset to Buffer #44
Comments
You must note, that this only works for non-negated bitsets. If // Remove unnecessary zeros
while (bs.data.length > 1 && bs.data[bs.data.length - 1] === 0) {
bs.data.pop();
}
// Pad with zeros to a multiple of 8
while (bs.data.length % 8 != 0) {
bs.data.push(0);
}
const buf = Buffer.from(bs.data.reverse()); If you don't need the minimal multiple of 8, remove the first loop. |
@infusion, eg. bs1.setRange(32,33,1); => 11 00000000000000000000001111110000 setRange 1 00000000000000000000000000000011 00000000000000000000001111110000 note: bs.toString() removes initial zeros. |
I know, I designed it like this.
I know. What has that to do with your initial question? |
let bitset = require("bitset");
let bs = bitset(buf); //convert back to buffer <Buffer 00 00 00 00 00 00 00 64> So we are not getting same buffer... @infusion |
As we can construct a bitset object by a Buffer, Can we convert the bitset to Buffer
let bitset = require("bitset");
let buf = Buffer.from([100, 97, 98]);
let bs = bitset(buf);
bs.toString();
'11000100110000101100100'
Can we convert a bitset into buffer?
let bs1 = new bitset();
bs1.setRange(4,9,1);
{ data: [ 1008, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], _: 0 }
bs1.toString();
'1111110000'
Now can this be converted into the buffer?
eg. add the padding 0's left and make it's size multiple of 8 then convert to byte array or Buffer.
The text was updated successfully, but these errors were encountered: