简体   繁体   中英

javascript bigint to byte array

The new javascript native BigInt implementation describes an abstract method NumberToRawBytes suggesting a convenient way to create byte arrays wiht explicit endianness:

https://tc39.github.io/proposal-bigint/#sec-typedarrays-and-dataview

Is there a way to use it already in the js API? Does javascript have some "to array" interface that implements it, for example?

Looking into the lib.esnext.bigint.d.ts file in Typescript under /lib, I found at the bottom "interface DataView".

I THINK this is what you and I are looking for!

interface DataView {
    /**
      * Gets the BigInt64 value at the specified byte offset from the start of the view. There is
      * no alignment constraint; multi-byte values may be fetched from any offset.
      * @param byteOffset The place in the buffer at which the value should be retrieved.
      */
    getBigInt64(byteOffset: number, littleEndian?: boolean): bigint;

    /**
      * Gets the BigUint64 value at the specified byte offset from the start of the view. There is
      * no alignment constraint; multi-byte values may be fetched from any offset.
      * @param byteOffset The place in the buffer at which the value should be retrieved.
      */
    getBigUint64(byteOffset: number, littleEndian?: boolean): bigint;

    /**
      * Stores a BigInt64 value at the specified byte offset from the start of the view.
      * @param byteOffset The place in the buffer at which the value should be set.
      * @param value The value to set.
      * @param littleEndian If false or undefined, a big-endian value should be written,
      * otherwise a little-endian value should be written.
      */
    setBigInt64(byteOffset: number, value: bigint, littleEndian?: boolean): void;

    /**
      * Stores a BigUint64 value at the specified byte offset from the start of the view.
      * @param byteOffset The place in the buffer at which the value should be set.
      * @param value The value to set.
      * @param littleEndian If false or undefined, a big-endian value should be written,
      * otherwise a little-endian value should be written.
      */
    setBigUint64(byteOffset: number, value: bigint, littleEndian?: boolean): void;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM