小程序不支持TextDecoder?
发布于 6 年前 作者 xiulan82 7898 次浏览 来自 官方Issues

下载云存储的json文件,涉及汉字转码问题,开发工具上完整运行

let gbk = new TextDecoder('gbk').decode(new Uint8Array(res.data)) //这里把gbk文件读入转换成了utf8的String

真机上

TypeError: TextDecoder is not a constructor

求高手解答

1 回复

https://github.com/inexorabletash/text-encoding下载了encoding.js。

由于码表500多k,我只需要gbk转utf8,所以仅仅保留了码表中gb18030,剩余141k。

var encoding = require(“encoding.js”);

let gbk = new encoding.TextDecoder(‘gbk’).decode(new Uint8Array(res.data))

问题解决,希望对遇到类似问题的有所帮助,所以写在这里。

这个转换没有数学关系,所以网上搜索的各种代码基本无效。

回到顶部