最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uint16Array to Uint8Array - Stack Overflow

programmeradmin3浏览0评论

I have a basic question. Say I have a Uint16Array and I have number 4 in it.

data_16=new Uint16Array([4]);  

Now I have a length 1 and byteLength 2;

how do i convert this to Uint8Array.

I do not want to create a new view.

data_8 = new Uint8Array(data_16)

If I do this I get array length 1 and byteLength 1. This is not what I want.

I need to stretch that 16 bit value in 16array into 8 bit values so that 8bit array so it would end up with 2 values int 8 bit array.

I can just create a funtion which converts with shif and to all that stuff. But Can it be done with Array manipulation only?

I have a basic question. Say I have a Uint16Array and I have number 4 in it.

data_16=new Uint16Array([4]);  

Now I have a length 1 and byteLength 2;

how do i convert this to Uint8Array.

I do not want to create a new view.

data_8 = new Uint8Array(data_16)

If I do this I get array length 1 and byteLength 1. This is not what I want.

I need to stretch that 16 bit value in 16array into 8 bit values so that 8bit array so it would end up with 2 values int 8 bit array.

I can just create a funtion which converts with shif and to all that stuff. But Can it be done with Array manipulation only?

Share Improve this question asked Nov 20, 2014 at 11:40 Evren BingølEvren Bingøl 1,7252 gold badges28 silver badges44 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

You can use the buffer property, i.e. data_8 = new Uint8Array(data_16.buffer, data_16.byteOffset, data_16.byteLength).

Would like to note that the current accepted answer may not work because of the byte order is platform dependent.

发布评论

评论列表(0)

  1. 暂无评论