JsPack is a library for working with binary data in JavaScript using the Uint8Array object. To ensure maximum stability, this library has 100% test coverage. The library was created using typescript for more convenient work with many types.
Install with npm:
npm i jspack.ts
Key | Length | Type | Description | Examples | |
---|---|---|---|---|---|
A | any | number[] | Array | Pack | Unpack |
x | 1 | number | NUL-padded byte | Pack | Unpack |
c | 1 | string | Single char (ascii) | Pack | Unpack |
s | any | string | String char (ascii) | Pack | Unpack |
b | 1 | number | Char (signed) | Pack | Unpack |
B | 1 | number | Char (unsigned) | Pack | Unpack |
h | 2 | number | Short (signed) | Pack | Unpack |
H | 2 | number | Short (unsigned) | Pack | Unpack |
i | 4 | number | Int (signed) | Pack | Unpack |
I | 4 | number | Int (unsigned) | Pack | Unpack |
l | 4 | number | Long (signed) | Pack | Unpack |
L | 4 | number | Long (unsigned) | Pack | Unpack |
q | 8 | number | Long long (signed) | Pack | Unpack |
Q | 8 | number | Long long (unsigned) | Pack | Unpack |
f | 4 | numberยน | Float | Pack | Unpack |
d | 8 | numberยน | Double | Pack | Unpack |
1: Unpack with 'f' and 'd' key can return NaN. The value NAN is used to represent a value that is an error. This is represented when exponent field is all ones with a zero sign bit or a mantissa that it not 1 followed by zeros. This is a special value that might be used to denote a variable that doesnโt yet hold a value.
Key | Description |
---|---|
< | Little endian |
> | Big endian |
! | Network (big endian) |
import { JSPack, JSPackFormat, JSPackEndianness } from 'jspack.ts';
const { JSPack, JSPackFormat, JSPackEndianness } = require('jspack.ts');
- Char (ascii)
- Int
- Int64 (long long int)
- String (ASCII chars)
- Array (Raw data)
- IEEE 754 (Float, Double)
- Null Byte
// Return Uint8Array(97)
JSPack.Pack(JSPackFormat.c, 'a', JSPackEndianness.bigEndian);
// Return 'b'
JSPack.Unpack(JSPackFormat.c, new Uint8Array([98]), JSPackEndianness.bigEndian);
// Return Uint8Array(0x85)
JSPack.Pack(JSPackFormat.b, -123, JSPackEndianness.bigEndian);
// Return -123
JSPack.Unpack(JSPackFormat.b, new Uint8Array([0x85]), JSPackEndianness.bigEndian);
// Return Uint8Array(0x00, 0x01, 0xEE, 0x0D, 0x32, 0xDE, 0xBC, 0xD1)
const longLongInt = {
low: 0x32DEBCD1,
high: 0x0001EE0D,
unsigned: true,
};
JSPack.Pack(JSPackFormat.q, longLongInt, JSPackEndianness.bigEndian);
const longLongIntBinary = new Uint8Array([0x00, 0x01, 0xEE, 0x0D, 0x32, 0xDE, 0xBC, 0xD1]);
// Return { low: 0x32DEBCD1, high: 0x0001EE0D, unsigned: true }
JSPack.Unpack(JSPackFormat.q, longLongIntBinary, JSPackEndianness.bigEndian);
// Return Uint8Array(97, 98, 99)
JSPack.Pack(JSPackFormat.s, 'abc', JSPackEndianness.bigEndian);
// Return 'abc'
JSPack.Unpack(JSPackFormat.s, new Uint8Array([97, 98, 99]), JSPackEndianness.bigEndian);
// Return Uint8Array(0x61, 0x62, 0x63)
JSPack.Pack(JSPackFormat.A, [97, 98, 99], JSPackEndianness.bigEndian);
// Return [97, 98, 99]
JSPack.Unpack(JSPackFormat.A, new Uint8Array([0x61, 0x62, 0x63]), JSPackEndianness.bigEndian);
// Return Uint8Array(0x47, 0x59, 0x3, 0x55)
JSPack.Pack(JSPackFormat.f, 55555.333, JSPackEndianness.bigEndian);
// Return 55555.33203125
JSPack.Unpack(JSPackFormat.f, new Uint8Array([0x47, 0x59, 0x3, 0x55]), JSPackEndianness.bigEndian);
// Return Uint8Array(0)
JSPack.Pack(JSPackFormat.x, 0x0, JSPackEndianness.bigEndian);
// Return 0
JSPack.Unpack(JSPackFormat.x, new Uint8Array([0]), JSPackEndianness.bigEndian);