我通过var uni = new Uint8Array([255, 216, 255, 0, 0, 0, 0, 0])创建了一个数组缓冲区,但是当我试图使用map uni.map(byte => byte.toString(16))取回字节时,它会返回Uint8Array(8) [0, 0, 0, 0, 0, 0, 0, 0]
发布于 2018-09-18 04:41:07
Uint8Array只能包含8位无符号整数(0到255之间的整数).当您使用Uint8Array.protoype.map()方法试图将每个元素转换为字符串时,它们超出了0,255的范围,并被设置为0。
如果要将它们转换为字符串,则首先将Uint8Array转换为普通的Array,然后使用Array.prototype.map()
const array = Array.from(uni)
array.map(byte => byte.toString(16))发布于 2018-09-18 04:29:20
您必须首先将TypedArray转换为Array:Array.prototype.slice.call(uni).map(byte=>byte.toString(16))
https://stackoverflow.com/questions/52378917
复制相似问题