Package Exports
- jspack.ts
- jspack.ts/dist/main.js
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 (jspack.ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
JsPack.ts
Library to pack/unpack binary data
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.
Installation
Install with npm:
npm i jspack.ts
Formats
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 |
Superscripts:
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.
Endianness
Key | Description |
---|---|
< | Little endian |
> | Big endian |
! | Network (big endian) |
Initialization
ES6:
import { JSPack, JSPackFormat, JSPackEndianness } from 'jspack.ts';
Node:
const { JSPack, JSPackFormat, JSPackEndianness } = require('jspack.ts');
Examples
Formats:
- Char (ascii)
- Int
- Int64 (long long int)
- String (ASCII chars)
- Array (Raw data)
- IEEE 754 (Float, Double)
- Null Byte
Char (ascii)
Pack:
// Return Uint8Array(97)
JSPack.Pack(JSPackFormat.c, 'a', JSPackEndianness.bigEndian);
Unpack:
// Return 'b'
JSPack.Unpack(JSPackFormat.c, new Uint8Array([98]), JSPackEndianness.bigEndian);
Int
Pack:
// Return Uint8Array(0x85)
JSPack.Pack(JSPackFormat.b, -123, JSPackEndianness.bigEndian);
Unpack:
// Return -123
JSPack.Unpack(JSPackFormat.b, new Uint8Array([0x85]), JSPackEndianness.bigEndian);
Int64 (long long int)
Pack:
// 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);
Unpack:
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);
String (ASCII chars)
Pack:
// Return Uint8Array(97, 98, 99)
JSPack.Pack(JSPackFormat.s, 'abc', JSPackEndianness.bigEndian);
Unpack:
// Return 'abc'
JSPack.Unpack(JSPackFormat.s, new Uint8Array([97, 98, 99]), JSPackEndianness.bigEndian);
Array (Raw data)
Pack:
// Return Uint8Array(0x61, 0x62, 0x63)
JSPack.Pack(JSPackFormat.A, [97, 98, 99], JSPackEndianness.bigEndian);
Unpack:
// Return [97, 98, 99]
JSPack.Unpack(JSPackFormat.A, new Uint8Array([0x61, 0x62, 0x63]), JSPackEndianness.bigEndian);
IEEE 754 (Float, Double)
Pack:
// Return Uint8Array(0x47, 0x59, 0x3, 0x55)
JSPack.Pack(JSPackFormat.f, 55555.333, JSPackEndianness.bigEndian);
Unpack:
// Return 55555.33203125
JSPack.Unpack(JSPackFormat.f, new Uint8Array([0x47, 0x59, 0x3, 0x55]), JSPackEndianness.bigEndian);
Null Byte
Pack:
// Return Uint8Array(0)
JSPack.Pack(JSPackFormat.x, 0x0, JSPackEndianness.bigEndian);
Unpack:
// Return 0
JSPack.Unpack(JSPackFormat.x, new Uint8Array([0]), JSPackEndianness.bigEndian);