Convert to/from ES-2020 native JS implementation of BigInt from/to:
Buffer
(node.js) orArrayBuffer|TypedArray
(native js),- hex
string
, - utf8-encoded text
string
.
It provides a common interface for the conversions that works for both node.js and native javascript.
Note that there is not a directly visible
TypedArray()
constructor, but a set of typed array ones:Int8Array()
,Uint8Array()
,Uint8ClampedArray()
,Int16Array()
,Uint16Array()
,Int32Array()
,Uint32Array()
,Float32Array()
,Float64Array()
,BigInt64Array()
,BigUint64Array()
.
bigint-conversion is distributed for web browsers and/or webviews supporting BigInt as an ES6 module or an IIFE file; and for Node.js as a CJS module.
bigint-conversion can be imported to your project with npm
:
npm install bigint-conversion
NPM installation defaults to the ES6 module for browsers and the CJS one for Node.js. For web browsers, you can also directly download the IIFE bundle or the ESM bundle from the repository.
Import your module as :
- Node.js
const bigintConversion = require('bigint-conversion') ... // your code here
- JavaScript native or TypeScript project
import * as bigintConversion from 'bigint-conversion' ... // your code here
BigInt is ES-2020. In order to use it with TypeScript you should set
lib
(and probably alsotarget
andmodule
) toesnext
intsconfig.json
. - JavaScript native browser ES6 mod
<script type="module"> import * as bigintConversion from 'lib/index.browser.bundle.mod.js' // Use you actual path to the broser mod bundle ... // your code here </script>
- JavaScript native browser IIFE
<script src="../../lib/index.browser.bundle.iife.js"></script> <!-- Use you actual path to the browser bundle --> <script> ... // your code here </script>
Converts a bigint to an ArrayBuffer or a Buffer (default for Node.js)
Kind: global function
Returns: ArrayBuffer
| Buffer
- An ArrayBuffer or a Buffer with a binary representation of the input bigint
Param | Type | Default | Description |
---|---|---|---|
a | bigint |
||
[returnArrayBuffer] | boolean |
false |
In Node JS forces the output to be an ArrayBuffer instead of a Buffer (default). |
Converts an ArrayBuffer, TypedArray or Buffer (node.js) to a bigint
Kind: global function
Returns: bigint
- A BigInt
Param | Type |
---|---|
buf | ArrayBuffer | TypedArray | Buffer |
Converts a bigint to a hexadecimal string
Kind: global function
Returns: str
- A hexadecimal representation of the input bigint
Param | Type |
---|---|
a | bigint |
Converts a hexadecimal string to a bigint
Kind: global function
Returns: bigint
- A BigInt
Param | Type |
---|---|
hexStr | string |
Converts a bigint representing a binary array of utf-8 encoded text to a string of utf-8 text
Kind: global function
Returns: string
- A string text with utf-8 encoding
Param | Type | Description |
---|---|---|
a | bigint |
A bigint representing a binary array of utf-8 encoded text. |
Converts a utf-8 string to a bigint (from its binary representaion)
Kind: global function
Returns: bigint
- A bigint representing a binary array of the input utf-8 encoded text
Param | Type | Description |
---|---|---|
text | string |
A string text with utf-8 encoding |
Converts an ArrayBuffer, TypedArray or Buffer (in Node.js) containing utf-8 encoded text to a string of utf-8 text
Kind: global function
Returns: string
- A string text with utf-8 encoding
Param | Type | Description |
---|---|---|
buf | ArrayBuffer | TypedArray | Buffer |
A buffer containing utf-8 encoded text |
Converts a string of utf-8 encoded text to an ArrayBuffer or a Buffer (default in Node.js)
Kind: global function
Returns: ArrayBuffer
| Buffer
- An ArrayBuffer or a Buffer containing the utf-8 encoded text
Param | Type | Default | Description |
---|---|---|---|
str | string |
A string of text (with utf-8 encoding) | |
[returnArrayBuffer] | boolean |
false |
In Node JS forces the output to be an ArrayBuffer instead of a Buffer (default). |
Returns the hexadecimal representation of a buffer.
Kind: global function
Returns: string
- A string with a hexadecimal representation of the input buffer
Param | Type |
---|---|
buf | ArrayBuffer | TypedArray | Buffer |
Converts a hexadecimal string to a buffer
Kind: global function
Returns: ArrayBuffer
| Buffer
- An ArrayBuffer or a Buffer
Param | Type | Default | Description |
---|---|---|---|
hexStr | string |
A string representing a number with hexadecimal notation | |
[returnArrayBuffer] | boolean |
false |
In Node JS forces the output to be an ArrayBuffer instead of a Buffer (default). |
TypedArray : Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array
A TypedArray object describes an array-like view of an underlying binary data buffer.
Kind: global typedef