Uint16Array to Uint8Array

隐身守侯 提交于 2019-12-22 10:35:14

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/27038704/uint16array-to-uint8array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!