dataview中写入对象_DataView对象

DataView是ArrayBuffer的视图,允许不同大小和类型的元素。它提供了读写方法来添加和获取数据,支持指定字节起始位置和长度。默认使用大尾数表示法,可通过参数切换为小尾数。注意,与类型数组不同,DataView允许控制字节序,适合需要精确控制数据编码的场景。
摘要由CSDN通过智能技术生成

dataview中写入对象

DataView is a view into an ArrayBuffer, like Typed Arrays, but in this case the items in the array can have different sizes and types.

DataViewArrayBuffer的视图,就像Typed Arrays一样 ,但是在这种情况下,数组中的项目可以具有不同的大小和类型。

Here’s an example:

这是一个例子:

const buffer = new ArrayBuffer(64)
const view = new DataView(buffer)

Since this is a view over a buffer, we can specify which byte we want to start from, and the length:

由于这是一个缓冲区视图,因此我们可以指定要从哪个字节开始以及长度:

const view = new DataView(buffer, 10) //start at byte 10
const view = new DataView(buffer, 10, 30) //start at byte 10, and add 30 items

If we don’t add those additional arguments, the view starts at position 0 and loads all the bytes present in the buffer.

如果不添加这些其他参数,则视图从位置0开始,并加载缓冲区中存在的所有字节。

There is a set of methods we can use to add data into the buffer:

我们可以使用一组方法将数据添加到缓冲区中:

  • setInt8()

    setInt8()

  • setInt16()

    setInt16()

  • setInt32()

    setInt32()

  • setUint8()

    setUint8()

  • setUint16()

    setUint16()

  • setUint32()

    setUint32()

  • setFloat32()

    setFloat32()

  • setFloat64()

    setFloat64()

This is how to call one of those methods:

这是调用这些方法之一的方法:

const buffer = new ArrayBuffer(64)
const view = new DataView(buffer)
view.setInt16(0, 2019)

By default data is stored using big endian notation. You can overwrite this setting and use little endian by adding a third parameter with the true value:

默认情况下,数据使用大尾数表示法存储。 您可以通过添加第三个参数为true值来覆盖此设置并使用little endian:

const buffer = new ArrayBuffer(64)
const view = new DataView(buffer)
view.setInt16(0, 2019, true)

Here is how we can get data from the view:

这是我们如何从视图中获取数据的方法:

  • getInt8()

    getInt8()

  • getInt16()

    getInt16()

  • getInt32()

    getInt32()

  • getUint8()

    getUint8()

  • getUint16()

    getUint16()

  • getUint32()

    getUint32()

  • getFloat32()

    getFloat32()

  • getFloat64()

    getFloat64()

Example:

例:

const buffer = new ArrayBuffer(64)
const view = new DataView(buffer)
view.setInt16(0, 2019)
view.getInt16(0) //2019

Since a DataView is an ArrayBufferView, we have those 3 read-only properties:

由于DataViewArrayBufferView ,因此我们具有这3个只读属性:

  • buffer points to the original ArrayBuffer

    buffer指向原始ArrayBuffer

  • byteOffset is the offset on that buffer

    byteOffset是该缓冲区的偏移量

  • byteLength is the length of its content in bytes

    byteLength是其内容的长度(以字节为单位)

One thing to keep in mind is that typed arrays don’t let us control the endianness: it uses the endianness of the system. In general this works out fine, because the main use case as we said is to use the array locally, using one of the multimedia APIs.

要记住的一件事是类型数组不允许我们控制字节序: 它使用系统的字节序 。 总的来说,这很好,因为我们所说的主要用例是使用多媒体API之一在本地使用数组。

If you transfer the data of a Typed Array on another system, the data might be not badly encoded if it uses Big Endian and you use Little Endian.

如果您在另一个系统上传输类型数组的数据,则使用Big Endian并使用Little Endian的数据可能编码不正确。

In case you need this kind of control, DataView is a perfect choice.

如果您需要这种控件, DataView是一个完美的选择。

翻译自: https://flaviocopes.com/dataview/

dataview中写入对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值