Package Exports
- byte-data
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (byte-data) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
byte-data
Readable data to and from byte buffers.
Copyright (c) 2017 Rafael da Silva Rocha.
https://github.com/rochars/byte-data
Install
npm install byte-data
For Node.js and the browser.
Support:
- booleans
- 2-bit integers (signed/unsigned)
- 4-bit integers (signed/unsigned)
- 8-bit integers (signed/unsigned)
- 16-bit integers (signed/unsigned)
- 16-bit half precision floating point numbers
- 24-bit integers (signed/unsigned)
- 32-bit integers (signed/unsigned)
- 32-bit single precision floating point numbers
- 40-bit integers (signed/unsigned)
- 48-bit integers (signed/unsigned)
- 64-bit double precision floating point numbers
- Strings
Example
let byteData = require('byte-data');
// Signed 24-bit integers from a byte buffer
let numbers = fromBytes(buffer, 24, {"signed": true});
// 16-bit half-precision float numbers from bytes represented as binary strings
console.log(
fromBytes(
["00110101", "01010101"],
16,
{"base": 2, "float": true}
)
);
// 0.33325
// 32-bit floating point numbers to bytes represented as hexadecimal values
toBytes([2.1474836], 32, {"base": 16, "float": true});
// ["5f","70","9","40"]
// 32-bit signed integers to and from binary form
byteData.toBytes([-2147483648, 2147483647], 32);
//[0,0,0,128,255,255,255,127]
byteData.fromBytes([0,0,0,128,255,255,255,127], 32, {"signed": true});
//[-2147483648, 2147483647]
Use
/**
* Turn numbers and strings to bytes.
* @param {!Array<number>|string} values The data.
* @param {number} bitDepth The bit depth of the data.
* Possible values are 1, 2, 4, 8, 16, 24, 32, 40, 48 or 64.
* @param {Object} options The options:
* - "float": True for floating point numbers. Default is false.
* This option is available for 16, 32 and 64-bit numbers.
* - "base": The base of the output. Default is 10. Can be 2, 10 or 16.
* - "char": If the bytes represent a string. Default is false.
* - "be": If the values are big endian. Default is false (little endian).
* - "buffer": If the bytes should be returned as a Uint8Array.
* Default is false (bytes are returned as a regular array).
* @return {!Array<number>|Uint8Array} the data as a byte buffer.
*/
toBytes(numbers, bitDepth);
/**
* Turn a byte buffer into what the bytes represent.
* @param {!Array<number>|Uint8Array} buffer An array of bytes.
* @param {number} bitDepth The bit depth of the data.
* Possible values are 1, 2, 4, 8, 16, 24, 32, 40, 48 or 64.
* @param {Object} options The options. They are:
* - "signed": If the numbers are signed. Default is false (unsigned).
* - "float": True for floating point numbers. Default is false.
* This option is available for 16, 32 and 64-bit numbers.
* - "base": The base of the input. Default is 10. Can be 2, 10 or 16.
* - "char": If the bytes represent a string. Default is false.
* - "be": If the values are big endian. Default is false (little endian).
* @return {!Array<number>|string}
*/
fromBytes(buffer, bitDepth);
/**
* Find and return the start index of some string.
* Return -1 if the string is not found.
* @param {!Array<number>|Uint8Array} bytes Array of bytes.
* @param {string} chunk Some string to look for.
* @return {number} The start index of the first occurrence, -1 if not found
*/
byteData.findString(bytes, "chunk");
Pack your nibbles
Packing nibbles:
byteData.packNibbles([15, 15, 1, 4, 1, 15]);
//[255, 20, 31]);
This will pack 2 nibbles into one byte.
Unpacking nibbles:
byteData.unpackNibbles([255, 20, 31]);
//[15, 15, 1, 4, 1, 15]
Pack your crumbs
Packing crumbs:
byteData.packCrumbs([3,3,3,3,1,2,3,0,1,1,0,0]);
//[255, 108, 80]);
This will pack 4 crumbs into one byte.
Unpacking crumbs:
byteData.unpackCrumbs([108]);
//[1, 2, 3, 0]
Pack your booleans
Packing booleans:
byteData.packBooleans([0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0]);
//[0,76]);
This will pack 8 booleans into one byte.
Unpacking booleans:
byteData.unpackBooleans([77]);
//[0,1,0,0,1,1,0,1]
LICENSE
Copyright (c) 2017 Rafael da Silva Rocha.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.