gpt4 book ai didi

javascript - 如何获取 DataView 类型的大小(例如 Uint32=4、Float64=8)以推进偏移量?

转载 作者:行者123 更新时间:2023-12-03 16:49:39 24 4
gpt4 key购买 nike

我正在用 DataView 解析序列化对象并希望能够根据数据大小增加偏移量变量。我不想为像 BYTES_PER_UINT16

这样的简单事情重新定义变量
...
var dv = new DataView(payload);
var offset = 0;
anObject.field1 = dv.getUint8(offset);
offset += BYTES_PER_UINT8;
anObject.field2 = dv.getUint32(offset, true);
offset += BYTES_PER_UINT32;
...

最佳答案

您需要将它们包装在一个为您执行此操作的对象中。

例如:

function DataViewExt(o) {
this.view = new DataView(o);
this.pos = 0;
}

DataViewExt.prototype = {
getUint8: function() {
return this.view.getUint8(this.pos++);
},

getUint16: function() {
var v = this.view.getUint16(this.pos);
this.pos += 2;
return v
},
// etc...
};

现在您可以创建实例了:

var dv = new DataViewExt(payload);
var uint8 = dv.getUint8(); // advances 1 byte
var uint16 = dv.getUint16(); // advances 2 bytes
...
console.log("Current position:", dv.pos);

修改以适应您的场景。

关于javascript - 如何获取 DataView 类型的大小(例如 Uint32=4、Float64=8)以推进偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26245032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com