将Uint8数组流转为单个字符串:问题及解决方法
将Uint8数组流转换为单个字符串的问题
我有一系列Uint8数组流,需要将其转换为单个字符串。尝试使用TextDecoderStream进行解码,虽能完成解码,但始终返回多个字符串而非单个。我的代码如下:
const textDecoder = new TextDecoderStream(); port.readable.pipeTo(textDecoder.writable); const reader = textDecoder.readable.getReader(); while (true) { const { value, done } = await reader.read(); if (done) { reader.releaseLock(); break; } console.log(value); }
请问有什么解决建议?
补充说明
最终我通过for循环和join函数得到了最终字符串,实现代码如下:
for (let i = 0; i < arrLength; i++) { const { value } = await reader.read(); if (value) { arr.push(value); } } const rfidString = arr.join("");
内容的提问来源于stack exchange,提问作者Adam Šulc
相关产品推荐
相关产品推荐

