- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到错误 Offset is outside the bounds of the DataView
对于以下代码
let data = [...] // some array of Int16
let buf = new ArrayBuffer(data.length);
let dataView = new DataView(buf);
data.forEach((b, i) => {
dataView.setInt16(i, b);
});
这是 Chrome 中的调试 View
i
是
47999
和我的
DataView
的缓冲区大小是
48000
.我在这里想念什么?
最佳答案
这是因为 Int16Array 每个元素有 2 个字节。所以它的 .length
将比它的缓冲区的实际大小小两倍,使用它的 .byteLength
来创建一个相同大小的新 ArrayBuffer。
此外,设置 int16 实际上会一次设置两个字节。
所以在某些时候,你的循环会尝试设置一个不存在的字节,它会抛出那个错误。
但这并不是您的代码的全部。由于 forEach()
的迭代值 i
是基于 TypedArray 的 .length
值,因此您还需要将其乘以 TypedArray 每个元素的字节数以在 DataView.setInt16
中设置正确的偏移量。
const data = new Int16Array( [ 0xFFFF, 0xFF00, 0x00FF, 0x000 ] );
console.log( "length:", data.length );
console.log( "byteLength:", data.byteLength );
const buf = new ArrayBuffer(data.byteLength);
const dataView = new DataView(buf);
data.forEach( (b, i) => {
dataView.setInt16( i * data.BYTES_PER_ELEMENT, b );
} );
console.log( new Int16Array( buf ) );
// -1, 255, -256, 0
DataView.setInt16( byteOffset, value, littleEndian )
的第三个参数,但是你也可以简单地做:
const data = new Int16Array( [ 0xFFFF, 0xFF00, 0x00FF, 0x000 ] );
const buf = data.buffer.slice();
// ensure they are not the same ArrayBuffer
data.fill( 0 );
console.log( "data: ", data ); // 0, 0, 0 ,0
console.log( "copy:", new Int16Array( buf ) );
// -1, 256, 255, 0
.map
交换值来比使用 DataView 更快。
const data = new Int16Array( [ 0xFFFF, 0xFF00, 0x00FF, 0x000 ] );
// check for the computer's endianness
const is_little_endian = new Uint8Array( new Uint32Array( [ 0x12345678 ] ).buffer )[ 0 ] === 0x78;
console.log( is_little_endian );
const buf = is_little_endian ?
data.map( (val) => (val<<8) | (val>>8) & 0xFF ).buffer : data.buffer.slice();
// ensure they are not the same ArrayBuffer
data.fill( 0 );
console.log( "data: ", data ); // 0, 0, 0 ,0
console.log( "copy:", new Int16Array( buf ) );
// -1, 255, -256, 0
关于javascript - 偏移量在 DataView 的范围之外,调试器显示它在范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63466278/
这是我的 Dataview.List 这是我的 Dataview.Dataview 我想显示我的 Dataview.dataview 中的元素,就像我的 Dataview.List 中的元素一样。我只
我想通过过滤现有的 DataView 创建一个新的 DataView,但是我遇到了麻烦,因为 DataView 没有 .AsEnumerable() 方法并且没有实现 IEnumerable{Data
我想用如下状态和功能扩展 DataView: export class MyDataView extends DataView { public position: number; p
我正在使用 dataview 并使用它的 skip 和 take 方法,这将占用 5 行并根据页码和页面大小跳过一些行。 //creating a dataview object and assign
我正在为我的图片库使用 ExtJS DataView。这是我的项目工具提示方式。它在 tpl 中。 new Ext.XTemplate( '', '',
我无法在不转换的情况下查询 DataView。 IsPresent 方法中的两行代码取自 "LINQ - Specified cast is not valid with dataview use "
在我的应用程序中,我使用数据 View 在动态传递过滤器选项的地方应用过滤器。如果有 2 个过滤器参数,则数据 View 应针对参数 1 进行过滤,然后按参数 2 进行过滤。我正在使用一种在 for
我在我的页面上显示来自 DataTable 的信息,并想添加一些排序功能,这比直接的列排序要稍微好一点。因此,我一直在尝试将 LinkButtons 放在我的 GridView 的 HeaderI
我有两个要排序的 DataView,在 dgtest1 中,我尝试对包含 Typeid != 25 的数据进行排序,在 dgtest2 中我试图只显示 Typeid == 25 处的数据。 当我单步执
DataView 对象没有像 DataTable 那样的 Rows 属性。 如何循环遍历 DataView 的行? 最佳答案 DataView 对象本身用于循环遍历 DataView 行。 DataV
我正在尝试关注 http://mleibman.github.com/SlickGrid/examples/example4-model.html本教程。我下载了所有必需的 .js 文件(core.j
如何在顶部而不是默认在底部添加新行,在 slickgrid dataview impelmentation 中也很感激有人提供删除行的示例。 最佳答案 这是一个示例函数,可以与示例 1-simple.
我有一个数据表。我想按列名“城市”对其默认 View 进行排序。我希望排序不区分大小写。 这是我的代码: DataTable dt = GetDataFromSource(); dt.DefaultV
我有一个包含一列(“利润”)的数据表。我想要的是获取此表中所有值的总和。我试图通过以下方式做到这一点...... DataTable dsTemp = new DataTable(); dsTemp.
我在 DataView.Sort 上遇到性能瓶颈。代码如下。 /// /// Filters the data table and returns a new data table
我正在尝试确定 DataView 是否不包含数据。 lsSql = "Select col1, col2 from table where tableId = " + Id; DataView l
我有一个 DataTable包含一些行。复制到 DataView .现在我有 List 形式的 ID .其中包含从 GridView 中选择的项目.现在我想过滤这个 DataView使用 AND 作为
我有一个包含一些列的 gridview,我需要按日期列对 gridview 进行排序。但我无法正确排序。这是我使用的代码: dt.DefaultView.Sort = "Meldingsdatum a
我想将数据 View rowfilter 值转换为数据表。我有一个有值(value)的数据集。现在我正在使用数据 View 过滤值。现在我想将数据 View 过滤器值转换为数据表。请帮我复制它....
我有一个数据 View 。 dataview 的来源是来自 sql server 的 view。Dataview 已排序。像这样: dv_building.Sort = "bld_number" 此
我是一名优秀的程序员,十分优秀!