admin管理员组文章数量:1429806
I have an array of 4 bytes. 32-bit unsigned little endian.
[ 123, 1, 0, 0]
I need help converting this to an integer. I tried below with no luck:
let arr = [ 123, 1, 0, 0 ];
let uint = new Uint32Array(arr);
console.log('INT :', uint);
I have an array of 4 bytes. 32-bit unsigned little endian.
[ 123, 1, 0, 0]
I need help converting this to an integer. I tried below with no luck:
let arr = [ 123, 1, 0, 0 ];
let uint = new Uint32Array(arr);
console.log('INT :', uint);
Share
Improve this question
edited Jul 26, 2016 at 15:45
Lightness Races in Orbit
386k77 gold badges666 silver badges1.1k bronze badges
asked Jul 26, 2016 at 15:28
DonDon
972 silver badges9 bronze badges
1
- 1 8 bytes? where are the other 4? – dandavis Commented Jul 26, 2016 at 15:41
1 Answer
Reset to default 6There are two ways:
If you know your browser is also little endian (almost always true these days), then you can do it like this:
const bytes = new Uint8Array([123, 1, 0, 0]);
const uint = new Uint32Array(bytes.buffer)[0];
console.log(uint);
If you think your browser might be running in a big endian environment and you need to do proper endian conversion, then you do this:
const bytes = new Uint8Array([123, 1, 0, 0]);
const dv = new DataView(bytes.buffer);
const uint = dv.getUint32(0, /* little endian data */ true);
console.log(uint);
本文标签: 32 bitConverting 32bit unsigned little endian to integer in javascriptStack Overflow
版权声明:本文标题:32 bit - Converting 32-bit unsigned little endian to integer in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745516550a2661576.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论