gpt4 book ai didi

javascript - 为什么我在缓冲区上的 Javascript View 全为零?

转载 作者:行者123 更新时间:2023-11-30 14:08:23 25 4
gpt4 key购买 nike

我创建了一个缓冲区,然后在其上创建了一个 Uint8Array,但该数组没有任何值。我希望它具有缓冲区的值。这是一个易于重现的示例:

var buf = new ArrayBuffer(32);
for (var index = 0; index < 32; index++) buf[index] = index;
console.log(buf);
var arr = new Uint8Array(buf);
console.log(arr);

我在现实中尝试的是这样的日期格式转换器:

//Buffers and views
function convertDateTimeToFormat(date, initialFormat, endFormat) {
var buf = new ArrayBuffer(14);
var result = new Uint8Array(buf);
console.log(date);
var initialPositions = {};
var endPositions = {};
var sizeSoFar = 0;
for (var c of initialFormat) {
if (c === 'y') {
initialPositions.y = new Uint32Array(date, sizeSoFar, 1);
} else {
initialPositions[c] = new Uint8Array(date, sizeSoFar, 2);
}
sizeSoFar += ((c === 'y') ? 4 : 2);
}
sizeSoFar = 0;
for (var c of endFormat) {
if (c === 'y') {
endPositions.y = new Uint32Array(buf, sizeSoFar, 1);
} else {
endPositions[c] = new Uint8Array(buf, sizeSoFar, 2);
}
sizeSoFar += ((c === 'y') ? 4 : 2);
}
for (var key in initialPositions) {
var limit = (key === 'y') ? 4 : 2;
for (var index = 0; index < limit; index++) endPositions[c][index] = initialPositions[c][index];
}
return result;
}
//2019-03-01 13:03:50
var buf = new ArrayBuffer( 14 );
buf[0] = 2;
buf[1] = 0;
buf[2] = 1;
buf[3] = 9;
buf[4] = 0;
buf[5] = 3;
buf[6] = 0;
buf[7] = 1;
buf[8] = 1;
buf[9] = 3;
buf[10] = 0;
buf[11] = 3
buf[12] = 5;
buf[13] = 0;
console.log(convertDateTimeToFormat(buf, "yMdHms", "yMdHms"));
console.log(convertDateTimeToFormat(buf, "yMdHms", "MdyHms"));

但是由于我在这个问题开头描述的行为,结果全为零。

这行得通,但不够优雅,因为它需要日期格式,如果我要确保输入与日期格式无关,那么代码将变得非常复杂:

//Buffers and views
var results = {};
var buf = new ArrayBuffer( 4 );
results.uint32 = new Uint32Array(buf);
results.int8 = new Uint8Array(buf);
results.uint8 = new Int8Array(buf);
results.int8[2] = -1;
console.log(results);
results.int8[2] = 0;
results.int8[1] = -1;
console.log(results);
results.int8[1] = 0;
results.int8[0] = -1;
console.log(results);

//Buffers and views
function convertDateTimeToFormat(date, format) {
var buf = new ArrayBuffer(14);
var result = new Uint8Array(buf);
var positions = {
y: 0,
M: 4,
d: 6,
H: 8,
m: 10,
s: 12
};
for (var index = 0; index < 14; index++) {
result[index] = date[positions[format[index]]++];
}
return result.join("");
}
var results = {};
//2019-03-01 13:03:50
var buf = new ArrayBuffer( 14 );
buf[0] = 2;
buf[1] = 0;
buf[2] = 1;
buf[3] = 9;
buf[4] = 0;
buf[5] = 3;
buf[6] = 0;
buf[7] = 1;
buf[8] = 1;
buf[9] = 3;
buf[10] = 0;
buf[11] = 3
buf[12] = 5;
buf[13] = 0;
console.log(convertDateTimeToFormat(buf, "yyyyMMddHHmmss"));
console.log(convertDateTimeToFormat(buf, "MMddyyyyHHmmss"));

最佳答案

ArrayBuffer只是一个代表内存片段的对象。它有一个固定的大小,就是这样。它具有表示内容的属性,因为您需要一个类型化数组作为缓冲区 View 。您的代码只是将属性分配给作为对象工作的缓冲区,但它实际上并不操作保持为零的字节内容。

如果您不需要缓冲区,则完全不要显式实例化它。随便写

var arr = new Uint8Array(32);
for (var index = 0; index < 32; index++) arr[index] = index;
console.log(arr.buffer);
console.log(arr);

在您的实际代码中,您似乎想要使用 Uint8Array 将数字存储在单独的字节中。也许您应该只将该数组而不是底层缓冲区传递给函数。

你可以这样创建它:

const arr = Uint8Array.of(2,0,1,9,0,3,0,1,1,3,0,3,5,0);
// or Uint8Array.from([2,0,1,9,0,3,0,1,1,3,0,3,5,0])
// or new Uint8Array(2,0,1,9,0,3,0,1,1,3,0,3,5,0]);
const buf = arr.buffer;

关于javascript - 为什么我在缓冲区上的 Javascript View 全为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54925376/

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