首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用纯JavaScript将GBK转换为UTF8?

如何使用纯JavaScript将GBK转换为UTF8?
EN

Stack Overflow用户
提问于 2013-06-20 18:52:32
回答 2查看 3.4K关注 0票数 0

我想从其他网站加载的内容是GBK编码的一些文本,但我的网站是UTF8。

有没有办法将这些GBK文本转换成UTF8进行显示?

出于某些原因,我只能使用JavaScript。

EN

回答 2

Stack Overflow用户

发布于 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功能。

票数 1
EN

Stack Overflow用户

发布于 2015-02-15 19:51:03

http://updates.html5rocks.com/2014/08/Easier-ArrayBuffer---String-conversion-with-the-Encoding-API

对于chrome或firefox,您可以使用TextDecoder将任何文本解码为unicode:

代码语言:javascript
复制
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'); 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17211780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档