@zoltu/bigint-helpers
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

Helper functions for ES2019 bigint primitive

A 0-dependency library that contains tools for (de)serializing native ES bigint values into/out of Uint8Arrays or hex strings for transport over some channel. It encodes using big endian and twos-complement. Supports numbers of any size that bigint supports, tested on nodejs up to 256-bits.

Serialization

unsignedBigintToUint8Array(0n, 8) // [0x00] as Uint8Array
signedBigIntToUint8Array(0n, 8) // [0x00] as Uint8Array
unsignedBigintToUint8Array(255n, 8) // [0xff] as Uint8Array
signedBigintToUint8Array(-1n, 8) // [0xff] as Uint8Array
unsignedBigintToHexString(255n, 8) // 'ff' as string
signedBigintToHexString(-1n, 8) // 'ff' as string
unsignedBigintToUint8Array(255n, 16) // [0x00, 0xff] as Uint8Array
signedBigintToUint8Array(-1n, 16) // [0xff, 0xff] as Uint8Array
unsignedBigintToHexString(255n, 16) // '00ff' as string
signedBigintToHexString(-1n, 16) // 'ffff' as string

Deserialization

uint8ArrayToUnsignedBigint(new Uint8Array([0x00])) // 0n
uint8ArrayToSignedBigint(new Uint8Array([0x00])) // 0n
uint8ArrayToUnsignedBigint(new Uint8Array([0xff])) // 255n
uint8ArrayToSignedBigint(new Uint8Array([0xff])) // -1n
hexStringToUnsignedBigint('ff') // 255n
hexStringToSignedBigint('ff') // -1

Readme

Keywords

none

Package Sidebar

Install

npm i @zoltu/bigint-helpers

Weekly Downloads

0

Version

2.0.1

License

Unlicense

Unpacked Size

29.7 kB

Total Files

11

Last publish

Collaborators

  • micah.zoltu