Skip to content

Instantly share code, notes, and snippets.

@jhonsore
Created January 26, 2016 13:40
Show Gist options
  • Save jhonsore/524b7821c431a39fb2ab to your computer and use it in GitHub Desktop.
Save jhonsore/524b7821c431a39fb2ab to your computer and use it in GitHub Desktop.
Converts ANSI characters to UTF-8
//Converte sequência de caracteres ANSI para UTF-8 e vice-versa.
//http://jsfromhell.com/pt/geral/utf-8
var UTF8 = {
encode: function(s){
for(var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]
);
return s.join("");
},
decode: function(s){
for(var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
((a = s[i][c](0)) & 0x80) &&
(s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
);
return s.join("");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment