UTF-8 Converter //JavaScript Repository

Description

Converts a sequence of ANSI characters to UTF-8 and vice-versa.
Created: 2007.10.07

Code (Download)

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/geral/utf-8 [rev. #1]

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("");
    }
};

Example (Example)

<script type="text/javascript">
//<![CDATA[

var s = "a????e";
document.write(
    ('UTF8.encode("' + s + '") = ').bold(), UTF8.encode(s), "<br />",
    ('UTF8.decode(UTF8.encode("' + s + '"))) = ').bold(), UTF8.decode(UTF8.encode(s))
);

//]]>
</script>

Help

UTF8.encode(s: String): String
Converts from ANSI to UTF-8
s
string to be converted
UTF8.decode(s: String): String
Converts from UTF-8 to ANSI
s
string to be converted

Rank (Votes: 106)

4.10