twos-complement-buffer
Copyright (c) 2018-2019 Rafael da Silva Rocha.
https://github.com/rochars/twos-complement-buffer
- MIT licensed
- Can be used where typed arrays can't
Install
``` npm install twos-complement-buffer ```Use
Browser
Use the twos-complement-buffer.js file in the /dist folder: ```html ``` Or load it from the jsDelivr CDN: ```html ``` Or load it from unpkg: ```html ```Node
```javascript const TwosComplementBuffer = require('twos-complement-buffer'); // A byte buffer, array and Uint8Array can be used let buffer = ; // Create a TwosComplementBuffer to handle 32-bit numbers let tcbParser = new TwosComplementBuffer(32); // Pack a value tcbParser.pack(buffer, -2045); // Check the buffer console.log(buffer); ```API
```javascript / A class to write and read two's complement signed integers to and from byte buffers. / class TwosComplementBuffer { /* @param {number} bits The number of bits used by the integer.
*/
constructor(bits) {
/**
* The number of bits used by one number.
* @type {number}
*/
this.bits;
/**
* The number of bytes used by one number.
* @type {number}
*/
this.bytes;
}
/
* Write one two's complement signed integer to a byte buffer.
* @param {!Uint8Array|!Array<number>} buffer An array of bytes.
* @param {number} num The number.
* @param {number=} index The index being written in the byte buffer.
* @return {number} The next index to write on the byte buffer.
* @throws {TypeError} If num is NaN.
* @throws {RangeError} On overflow.
*/
pack(buffer, num, index=0) {}
/
* Read one two's complement signed integer from a byte buffer.
* @param {!Uint8Array|!Array<number>} buffer An array of bytes.
* @param {number=} index The index to read.
* @return {number}
* @throws {RangeError} On overflow.
*/
unpack(buffer, index=0) {}
}
```