javascript bigint到byte数组

时间:2018-06-19 11:59:41

标签: javascript serialization bigint

新的JavaScript本机BigInt实现描述了一种抽象方法NumberToRawBytes,该方法建议了一种使用显式字节序创建字节数组的便捷方法:

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

在js API中已经可以使用它吗?例如,javascript是否具有一些实现该接口的“数组”接口?

1 个答案:

答案 0 :(得分:0)

在/ lib下的Typescript中,查看lib.esnext.bigint.d.ts文件,我在底部的“接口DataView”中找到了。

我想这就是你和我正在寻找的东西!

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;
}