node-bitstring
Version:
a very simple bit operation supply
59 lines (39 loc) • 1.96 kB
Markdown
a very simple bit operation module, this module is base created for dns header decode/encode.
npm install node-bitstring
```js
let BitString = require("node-bitstring");
// let bitOpt = new BitString(Buffer.alloc(2)); create a bit string with 32 bits
let bitOpt = new BitString(32);
//BitString {buffer: <Buffer 00 00 00 00>,binaryString: '00000000000000000000000000000000' }
//create a bit string with 32 bits
//should be a number which can divide by 8
//if not it will auto fill
bitOpt.set(0, 111111); //"11111100000000000000000000000000"
bitOpt.set(0, 000111); //"11000000000000000000000000000000"
bitOpt.set(0, "000111"); //"00011100000000000000000000000000"
// The number will conversion to the string if the number start will 0 it will be ignored
bitOpt.get(0, 10); //"0001110000"
bitOpt.get(0, 100); //"00011100000000000000000000000000"; if length out-of-bounds, the excess part will be ignored;
bitOpt.get(32, 100); //false; if index out-of-bounds, the excess part will be ignored;
bitOpt.buffer; // <Buffer 1c 00 00 00>
bitOpt.binaryString; // "00011100000000000000000000000000"
```
`BitString(data)` : `data` Can be either a number or buffer;
`BitString
`BitString
`BitString
```js
bitOpt.bufferToBinaryString(Buffer.alloc(4, "AABBCCDD", "hex")); //"10101010101110111100110011011101"
```
`BitString
```js
bitOpt
```
`BitString
`BitString
MIT