如何用纯Javascript将GBK转换为UTF8 ?

时间:2022-10-30 13:25:33

I want to load some text from other site wich the content is GBK encoded, but my site is UTF8.

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

Is there anyway by which I can convert these GBK text into UTF8 for display ?

我可以把这些GBK文本转换成UTF8来显示吗?

For some reasons I can only use Javascript for this.

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

2 个解决方案

#1


1  

http://www.1kjs.com/lib/widget/gbk/

http://www.1kjs.com/lib/widget/gbk/

There is a javascript to convert between gbk and unicode: which maps all the 21886 gbk Chinese chars to unicode

有一种javascript可以在gbk和unicode之间进行转换:它将所有的21886 gbk中文字符映射到unicode。

You can simply download the javascript file , include it and using :

unicode to gbk:

您可以简单地下载javascript文件,包括它和使用:unicode到gbk:

$URL.encode(unicode_string)

URL.encode美元(unicode_string)

or gbk to unicode:

或者gbk unicode:

$URL.decode(gbk_string)

URL.decode美元(gbk_string)

I tested it. It's working well on my web : zhuhaiyang.sinaapp.com/base64/index.html

我测试它。它在我的网站上运行良好:zhuhaiyang.sinaapp.com/base64/index.html。

which do base64 ency using pure javascript.

使用纯javascript实现base64。

#2


0  

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

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

For chrome or firefox, you could use TextDecoder to decode any text to unicode:

对于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'); 

#1


1  

http://www.1kjs.com/lib/widget/gbk/

http://www.1kjs.com/lib/widget/gbk/

There is a javascript to convert between gbk and unicode: which maps all the 21886 gbk Chinese chars to unicode

有一种javascript可以在gbk和unicode之间进行转换:它将所有的21886 gbk中文字符映射到unicode。

You can simply download the javascript file , include it and using :

unicode to gbk:

您可以简单地下载javascript文件,包括它和使用:unicode到gbk:

$URL.encode(unicode_string)

URL.encode美元(unicode_string)

or gbk to unicode:

或者gbk unicode:

$URL.decode(gbk_string)

URL.decode美元(gbk_string)

I tested it. It's working well on my web : zhuhaiyang.sinaapp.com/base64/index.html

我测试它。它在我的网站上运行良好:zhuhaiyang.sinaapp.com/base64/index.html。

which do base64 ency using pure javascript.

使用纯javascript实现base64。

#2


0  

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

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

For chrome or firefox, you could use TextDecoder to decode any text to unicode:

对于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');