简体   繁体   English

javascript bigint到byte数组

[英]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: 新的JavaScript原生BigInt实现描述了一个抽象方法NumberToRawBytes提出了一种通过显式字节序创建字节数组的便捷方法:

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

Is there a way to use it already in the js API? js API中已经可以使用它吗? Does javascript have some "to array" interface that implements it, for example? 例如,JavaScript是否具有一些实现该接口的“数组”接口?

Looking into the lib.esnext.bigint.d.ts file in Typescript under /lib, I found at the bottom "interface DataView". 在/ lib下的Typescript中查看lib.esnext.bigint.d.ts文件,我在底部的“接口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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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