byte-data
Pack and unpack binary data.
Copyright (c) 2017-2018 Rafael da Silva Rocha.
https://github.com/rochars/byte-data
byte-data is a JavaScript module for the serialization and deserialization of numbers and strings.
Version 13.x main differences from the latest release:
- Typed Arrays are used to encode/decode 32 and 64-bit floats. This makes this version overall faster than 15.x, but results will depend on the environment implementation of Typed Arrays, wich may differ from one to context to another.
- It only encode and decode ASCII characters; the current version includes support for UTF-8 characters.
This version (13.x) still receive updates and bug fixes.
- No dependencies
- MIT-licensed
- Use it out of the box in the browser
- Use it out of the box in Node
- Use it in little-endian and big-endian hosts
- Write to buffers, option to define start and end index to write
- Read from buffers, option to define start and end index to read
- Use typed arrays or arrays
Pack/unpack:
- Integers, unsigned and signed (two's complement)
- 16-bit IEEE half-precision floating point numbers
- 32-bit IEEE single-precision floating point numbers
- 64-bit IEEE double-precision floating point numbers
- Little-endian and big-endian words
- ASCII Strings (with validation)
Install
npm install byte-data
Use
Node
If you installed via NPM or Yarn, import byteData from byte-data:
import * as byteData from 'byte-data';
let packed = byteData.pack(128, {bits: 8});
Or import just what you need:
import {pack} from 'byte-data';
let packed = pack(128, {bits: 8});
Or require:
const byteData = require('byte-data');
let packed = byteData.pack(2.1474836, {bits: 32, float: true});
Browser
Use the compiled file in the /dist folder of this package:
<script src="./dist/byte-data.min.js"></script>
<script>
var packed = byteData.pack(2.1474836, {bits: 32, float: true});
</script>
Or get it from the jsDelivr CDN:
<script src="https://cdn.jsdelivr.net/npm/byte-data"></script>
Or get it from unpkg:
<script src="https://unpkg.com/byte-data"></script>
About
Floating-point numbers
Floating-point numbers are IEEE 754 standard.
Signed integers
Signed integers are two's complement.
Strings
Only ASCII characters are supported. Packing and unpacking strings with characters that are not ASCII will throw a 'Bad ASCII code.' error.
Overflow and underflow
Overflow or underflow on integers will throw "Overflow." and "Underflow." errors, respectively.
Browser compatibility
byte-data need IE10+ to run. All moderns browsers should work fine. Cross-browser tests are on the ROADMAP.
API
Strings
function unpackString(bytes, index=0, len=null) {}
function packString(str) {}
function packStringTo(str, bytes, index=0) {}
Numbers
function pack(value, theType) {}
function packArray(values, theType) {}
function packTo(value, theType, buffer, index=0) {}
function packArrayTo(values, theType, buffer, index=0) {}
function unpack(buffer, theType) {}
function unpackArray(buffer, theType) {}
function unpackFrom(buffer, theType, index=0) {}
function unpackArrayFrom(buffer, theType, start=0, end=null) {}
function unpackArrayTo(buffer, theType, output) {}
Types
Types are user-defined objects like this:
const float32 = {
bits: 32,
signed: true,
float: true,
be: false
}
There is a standard set of types that can be installed:
npm install binary-data-types
All types in binary-data-types are supported by byte-data. They are:
- int2
- uInt2
- int4
- uInt4
- int8
- uInt8
little-endian
- int16
- uInt16
- float16
- int24
- uInt24
- int32
- uInt32
- float32
- int40
- uInt40
- int48
- uInt48
- float64
big-endian:
- int16BE
- uInt16BE
- float16BE
- int24BE
- uInt24BE
- int32BE
- uInt32BE
- float32BE
- int40BE
- uInt40BE
- int48BE
- uInt48BE
- float64BE
Tests on big-endian systems
Use QEMU with this PowerPC/Debian image:
https://people.debian.org/~aurel32/qemu/powerpc/
Contributing
byte-data welcomes all contributions from anyone willing to work in good faith with other contributors and the community. No contribution is too small and all contributions are valued.
See CONTRIBUTING.md for details.
Style guide
byte-data code should follow the Google JavaScript Style Guide:
https://google.github.io/styleguide/jsguide.html
Code of conduct
This project is bound by a code of conduct: The Contributor Covenant, version 1.4, also available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting rocha.rafaelsilva@gmail.com.
Legal
LICENSE
Copyright (c) 2017-2018 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.