Loading

javascript - 将Float32Array转换为Int16Array

直接从ArrayBuffer转换

var dataAsInt16Array = new Int16Array(data.buffer);

var f32 = new Float32Array(4);
f32[0] = 0.1, f32[1] = 0.2, f32[2] = 0.3, f32[3] = 0.4;
// [0.10000000149011612, 0.20000000298023224, 0.30000001192092896, 0.4000000059604645]

//转换
var i16 = new Int16Array(f32.buffer);
// [-13107, 15820, -13107, 15948, -26214, 16025, -13107, 16076]

// 还原
new Float32Array(i16.buffer);
// [0.10000000149011612, 0.20000000298023224, 0.30000001192092896, 0.4000000059604645]
posted @ 2020-08-25 15:33  十年歧路  阅读(1953)  评论(0编辑  收藏  举报