How do I convert GBK to UTF8 with pure Javascript?

I want to load some text from other site wich 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.



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:


or gbk to unicode:


I tested it. It's working well on my web :

which do base64 ency using pure javascript.


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

function fetchAndDecode(file, encoding) {  
    var xhr = new XMLHttpRequest();'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); ;  
      } else {  
        console.error('Error while requesting', file, this);  



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.