Uint8Array在Javascript中字符串 [英] Uint8Array to string in Javascript
问题描述
我有一些UTF-8编码数据存在于Javascript中的一系列Uint8Array元素中。有没有一种有效的方法将这些解码为常规的javascript字符串(我相信Javascript使用16位Unicode)?我不希望当时添加一个字符,因为字符串concaternation将变为CPU密集型。
I have some UTF-8 encoded data living in a range of Uint8Array elements in Javascript. Is there an efficient way to decode these out to a regular javascript string (I believe Javascript uses 16 bit Unicode)? I dont want to add one character at the time as the string concaternation would become to CPU intensive.
推荐答案
TextEncoder
和 TextDecoder $来自编码标准的c $ c>,由 stringencoding库,在字符串和ArrayBuffers之间进行转换:
TextEncoder
and TextDecoder
from the Encoding standard, which is polyfilled by the stringencoding library, converts between strings and ArrayBuffers:
var uint8array = new TextEncoder("utf-8").encode("¢");
var string = new TextDecoder("utf-8").decode(uint8array);
这篇关于Uint8Array在Javascript中字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!