小程序
小游戏
企业微信
微信支付
扫描小程序码分享
下载云存储的json文件,涉及汉字转码问题,开发工具上完整运行
let gbk = new TextDecoder('gbk').decode(new Uint8Array(res.data)) //这里把gbk文件读入转换成了utf8的String
真机上
TypeError: TextDecoder is not a constructor
求高手解答
7 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
TextEncoder: var encoder = new TextEncoder() encoder.encode("中文abc"); //result : Uint8Array(9) [228, 184, 173, 230, 150, 135, 97, 98, 99] 兼容写法: unescape(encodeURIComponent("中文abc")).split("").map(val => val.charCodeAt()); //result : (9) [228, 184, 173, 230, 150, 135, 97, 98, 99] TextDecoder: var decoder = new TextDecoder(); decoder.decode(Uint8Array.from([228, 184, 173, 230, 150, 135, 97, 98, 99])); //result : 中文abc 兼容写法: decodeURIComponent(escape(String.fromCharCode(...[228, 184, 173, 230, 150, 135, 97, 98, 99]))); //result : 中文abc
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
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))
问题解决,希望对遇到类似问题的有所帮助,所以写在这里。
这个转换没有数学关系,所以网上搜索的各种代码基本无效。
转gbk 乱码可能是个bug。。需要改下encoding.js文件。
https://www.cnblogs.com/uncleguo/p/16186906.html
function utf8BytesToString(bytes) { if (!bytes || bytes.length === 0) return ''; // 如果传入的不是 Uint8Array,尝试转为 Uint8Array if (!(bytes instanceof Uint8Array)) { bytes = new Uint8Array(bytes); } let result = ''; let i = 0; while (i < bytes.length) { const byte1 = bytes[i]; if ((byte1 & 0x80) === 0) { // 1字节字符:0xxxxxxx (ASCII) result += String.fromCharCode(byte1); i += 1; } else if ((byte1 & 0xE0) === 0xC0) { // 2字节字符:110xxxxx 10xxxxxx if (i + 1 >= bytes.length) break; // 不足,跳过 const byte2 = bytes[i + 1]; if ((byte2 & 0xC0) !== 0x80) { i += 1; continue; // 非法 UTF-8,跳过 } const codePoint = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F); result += String.fromCharCode(codePoint); i += 2; } else if ((byte1 & 0xF0) === 0xE0) { // 3字节字符:1110xxxx 10xxxxxx 10xxxxxx if (i + 2 >= bytes.length) break; const byte2 = bytes[i + 1]; const byte3 = bytes[i + 2]; if ((byte2 & 0xC0) !== 0x80 || (byte3 & 0xC0) !== 0x80) { i += 1; continue; // 非法,跳过 } const codePoint = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F); result += String.fromCharCode(codePoint); i += 3; } else if ((byte1 & 0xF8) === 0xF0) { // 4字节字符:11110xxx 10xxxxxx 10xxxxxx 10xxxxxx // (可选支持,一般小程序/中文足够用 3 字节) if (i + 3 >= bytes.length) break; // 你可以选择继续实现 4 字节,但大多数情况下 3 字节足够覆盖中文 i += 1; continue; } else { // 非法 UTF-8 起始字节,跳过 i += 1; } } return result; } let text = utf8BytesToString(res.data);
感谢分享
被坑惨了,使用这破小程序PC版本解析protobuf,调试模式不闪退,PC版本真机一直闪退,就是由于readString()会调用自带的TextEncoder.decode()解析,导致闪退,按文章方式引入自定义js完美解决。
https://www.npmjs.com/package/pbf
readString
writeString
TextEncoder
TextDecoder
转完后您这边会出现乱码不?
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
TextEncoder: var encoder = new TextEncoder() encoder.encode("中文abc"); //result : Uint8Array(9) [228, 184, 173, 230, 150, 135, 97, 98, 99] 兼容写法: unescape(encodeURIComponent("中文abc")).split("").map(val => val.charCodeAt()); //result : (9) [228, 184, 173, 230, 150, 135, 97, 98, 99] TextDecoder: var decoder = new TextDecoder(); decoder.decode(Uint8Array.from([228, 184, 173, 230, 150, 135, 97, 98, 99])); //result : 中文abc 兼容写法: decodeURIComponent(escape(String.fromCharCode(...[228, 184, 173, 230, 150, 135, 97, 98, 99]))); //result : 中文abc
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))
问题解决,希望对遇到类似问题的有所帮助,所以写在这里。
这个转换没有数学关系,所以网上搜索的各种代码基本无效。
转gbk 乱码可能是个bug。。需要改下encoding.js文件。
https://www.cnblogs.com/uncleguo/p/16186906.html
function utf8BytesToString(bytes) { if (!bytes || bytes.length === 0) return ''; // 如果传入的不是 Uint8Array,尝试转为 Uint8Array if (!(bytes instanceof Uint8Array)) { bytes = new Uint8Array(bytes); } let result = ''; let i = 0; while (i < bytes.length) { const byte1 = bytes[i]; if ((byte1 & 0x80) === 0) { // 1字节字符:0xxxxxxx (ASCII) result += String.fromCharCode(byte1); i += 1; } else if ((byte1 & 0xE0) === 0xC0) { // 2字节字符:110xxxxx 10xxxxxx if (i + 1 >= bytes.length) break; // 不足,跳过 const byte2 = bytes[i + 1]; if ((byte2 & 0xC0) !== 0x80) { i += 1; continue; // 非法 UTF-8,跳过 } const codePoint = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F); result += String.fromCharCode(codePoint); i += 2; } else if ((byte1 & 0xF0) === 0xE0) { // 3字节字符:1110xxxx 10xxxxxx 10xxxxxx if (i + 2 >= bytes.length) break; const byte2 = bytes[i + 1]; const byte3 = bytes[i + 2]; if ((byte2 & 0xC0) !== 0x80 || (byte3 & 0xC0) !== 0x80) { i += 1; continue; // 非法,跳过 } const codePoint = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F); result += String.fromCharCode(codePoint); i += 3; } else if ((byte1 & 0xF8) === 0xF0) { // 4字节字符:11110xxx 10xxxxxx 10xxxxxx 10xxxxxx // (可选支持,一般小程序/中文足够用 3 字节) if (i + 3 >= bytes.length) break; // 你可以选择继续实现 4 字节,但大多数情况下 3 字节足够覆盖中文 i += 1; continue; } else { // 非法 UTF-8 起始字节,跳过 i += 1; } } return result; } let text = utf8BytesToString(res.data);
感谢分享
被坑惨了,使用这破小程序PC版本解析protobuf,调试模式不闪退,PC版本真机一直闪退,就是由于readString()会调用自带的TextEncoder.decode()解析,导致闪退,按文章方式引入自定义js完美解决。
https://www.npmjs.com/package/pbf
readString
andwriteString
to use HTML5TextEncoder
andTextDecoder
where available.转完后您这边会出现乱码不?