0

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

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

For some reasons I can only use JavaScript for this.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138

2 Answers2

1

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

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

unicode to gbk:

$URL.encode(unicode_string)

or gbk to unicode:

$URL.decode(gbk_string)

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

which do base64 ency using pure javascript.

diego zhu
  • 51
  • 1
  • 7
  • 3
    the link http://www.1kjs.com/lib/widget/gbk/ doesn't work anymore. Do you know of an alternate location? – ErikR Aug 01 '16 at 15:26
1

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:

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