Uint8Array在Javascript中字符串 [英] Uint8Array to string in Javascript

查看:618
本文介绍了Uint8Array在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆