我正在从服务器接收一个二进制数据包,其中包含以下内容:
var data = new Uint8Array([0xB2, 0xE2, 0xCA, 0xD4, 0x74, 0x65, 0x73, 0x74, 0x31, 0x32, 0x33]);我知道这是一个GBK字符集,我正在使用TextDecoder/TextEncoder来读取它:
var str = TextDecoder('gbk').decode(data);
// result: 测试test123现在,问题是:如何做相反的事情?
我测试了:
var data = TextEncoder().encode(str);
// but it doesn't match一些手握压缩:
(str.charCodeAt(0) & 0xff) | ((str.charCodeAt(1) >>> 8) & 0xff)
// but yeah, not need to be pro to know it can't works.有人知道怎么做反向操作吗?提前谢谢你。
发布于 2014-04-18 15:01:17
解决了。
我使用了shim:https://github.com/inexorabletash/text-encoding,并在TextEncoder构造函数中注释了一个条件部分,以允许其他字符集。
下面是一个示例:
if (this._encoding === null /*|| (this._encoding.name !== 'utf-8' &&
this._encoding.name !== 'utf-16le' &&
this._encoding.name !== 'utf-16be')*/)
throw new TypeError('Unknown encoding: ' + opt_encoding);https://stackoverflow.com/questions/23143231
复制相似问题