buffer-bits
This component is able to hold a set of bits (NOT bytes) and do operations on them. Could be used to analyze some protocol by yourself...
Quick Example
var Bits = ;var myBuffer = Buffer; // starting from bit offset 3// and capture 26 bits aftervar myBits = Bits; console;// 0b10100000111100010100000110
Features
- Class Methods
- Bits.from(buffer [, offset = 0 [, length = buffer.length << 3 ]])
- create Bits from existing Buffer object
- Bits.from(bits [, offset = 0 [, length = bits.length ]])
- create Bits from existing Bits object
- Bits.alloc(length)
- create Bits with count of bits and filled with 0
- Bits.from(buffer [, offset = 0 [, length = buffer.length << 3 ]])
- Constructor
- new Bits(buffer, offset, length) *
- there's NO params validating in this constructor, please DON'T use it for creating new instance...
- new Bits(buffer, offset, length) *
- Properties (read-only)
- length
- count of the total bits
- byteLength
- total bytes the Bits object holds
- isFullByte
- return true if total bits count is a multiple of 8
- buffer
- inner Buffer object hold by Bits. Take care to NOT write to this buffer directly.
- startOffset *
- represents the start bit offset inside the byte. This property should be RARELY used, use it when you know what you're doing...
- isLeftAligned *
- represents if all the bits are left aligned in the inner buffer, RARELY
- isRightAligned *
- represents if all the bits are right aligned in the inner buffer, RARELY
- length
- Methods
- readInt()
- read integer value of current bits (big endian)
- readIntLE()
- read integer value of current bits with little endian
- readUInt()
- read unsigned integer value of current bits (big endian)
- readUIntLE()
- read unsigned integer value of current bits with little endian
- readString([encoding[, start[, end]]])
- read string from the bits, same as buf.toString
- readBit(index)
- read the bit value (1: true, 0: false) at position 'index'
- setBit(index, value)
- set the bit value (1: true, 0: false) at position 'index'
- toggleBit(index)
- toggle the bit value at position 'index', 1->0, 0->1
- toggleAll()
- toggle all the bits
- concat(Bits)
- create Bits by concating another Bits object
- toBinaryString()
- return a '0b<binaryData>' format string, like 0b110101010
- equals(Bits)
- return true if all bits equals to another Bits object
- align(Boolean) *
- align all the bits inside the inner buffer to the left(
true
) or right(false
) side
- align all the bits inside the inner buffer to the left(
- alignLeft() *
- align all the bits in the inner buffer to the left side
- alignRight() *
- align all the bits in the inner buffer to the right side
- readInt()
More Examples
console;// 26 console;// 4 console;// 6 console;// <Buffer 02 83 c5 06> console;// 42190086 console;// false myBits;console;// true
More More Examples
Please check the tests in test
folder to see more details.
License
MIT