我想从其他网站加载的内容是GBK编码的一些文本,但我的网站是UTF8。
有没有办法将这些GBK文本转换成UTF8进行显示?
出于某些原因,我只能使用JavaScript。
发布于 2013-10-22 10:51:31
http://www.1kjs.com/lib/widget/gbk/
有一个javascript可以在gbk和unicode之间转换:which maps all the 21886 gbk Chinese chars to unicode
您可以简单地下载javascript file,将其包含在内,然后使用:
将unicode转换为gbk:
$URL.encode(unicode_string)
或gbk到unicode:
$URL.decode(gbk_string)
我测试过了。它在我的网站上运行良好: zhuhaiyang.sinaapp.com/base64/index.html
它使用纯javascript来执行base64功能。
发布于 2015-02-15 19:51:03
http://updates.html5rocks.com/2014/08/Easier-ArrayBuffer---String-conversion-with-the-Encoding-API
对于chrome或firefox,您可以使用TextDecoder将任何文本解码为unicode:
function fetchAndDecode(file, encoding) {
var xhr = new XMLHttpRequest();
xhr.open('GET', file);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
if (this.status == 200) {
var dataView = new DataView(this.response);
var decoder = new TextDecoder(encoding);
var decodedString = decoder.decode(dataView);
console.info(decodedString);
} else {
console.error('Error while requesting', file, this);
}
};
xhr.send();
}
fetchAndDecode('gbkencoded.txt','gbk'); https://stackoverflow.com/questions/17211780
复制相似问题