buffer-bits

0.0.12 • Public • Published

buffer-bits

Build Status

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 = require('buffer-bits');
var myBuffer = Buffer.from([20, 30, 40, 50]);
 
// starting from bit offset 3
// and capture 26 bits after
var myBits = Bits.from(myBuffer, 3, 26);    
 
console.log(myBits.toBinaryString());
// 0b10100000111100010100000110

Features

  1. 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
  2. Constructor
    • new Bits(buffer, offset, length) *
      • there's NO params validating in this constructor, please DON'T use it for creating new instance...
  3. 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
  4. 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]]])
    • 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
    • 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

More Examples

console.log(myBits.length);
// 26
 
console.log(myBits.byteLength);
// 4
 
console.log(myBits.startOffset);
// 6
 
console.log(myBits.buffer);
// <Buffer 02 83 c5 06>
 
console.log(myBits.readInt());
// 42190086
 
console.log(myBits.readBit(20));
// false
 
myBits.setBit(20, true);
console.log(myBits.readBit(20));
// true

More More Examples

Please check the tests in test folder to see more details.

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i buffer-bits

Weekly Downloads

2

Version

0.0.12

License

MIT

Last publish

Collaborators

  • strawhat