![](/img/trans.png)
[英]how to convert javascript BigInt to Uint8Array with negative values considered
[英]How to get an array of strings based on Uint8Array values (map Uint8 values to String/s)
new Uint8Array([1,2,3]).map((v,y)=>"0"+v+"0"+y)
> Uint8Array(3) [ 100, 201, 46 ] // actual
> ["0100","0201","0302"] // expected
new Uint8Array([1,2,3]).map((v,y)=>v+1)
> Uint8Array(3) [ 2, 3, 4 ] // actual as expected
[1, 2, 3].map(x => "0"+x);
> Array ["01", "02", "03"] // actual as expected
由于 302 大于 256 (Uint8 最大大小),因此转换后我们得到302%256
即46
。 把它作为一个证明我们没有得到任何数字类型,但特别是 Uint8。
那么如何获得基于 Uint8Array 值的字符串数组呢?
这里的问题是Uint8Array.map
返回一个Uint8Array
对象,您需要将其转换为普通数组才能获得您正在寻找的结果
const uint8Array = new Uint8Array([1,2,3]) const res1 = uint8Array.map((v,y)=>"0"+v+"0"+y) const res2 = [...uint8Array].map((v,y)=>"0"+v+"0"+y) console.log(res1, res2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.