Convert cryptic string to a readable one with JavaScript (UTF-8)

1.3k views Asked by At

I found out that when I save this distorted string ("Äußerungen üben") as an ANSI text file, then open it with Firefox and choose in the Firefox menu "Unicode", it turns it into a readable german format ("Äußerungen üben").

The same thing is possible with my text editor (Notepad++).

Is there any way to achieve this with JavaScript? E.g. the following would be nice:

var output = makeReadable("Äußerungen üben");

Unfortunately, I get this kind of distorted strings from an external source which doesn't care about UTF-8 and provides all data as ANSI.

PS: Saving the file as UTF-8 and setting the charset as UTF-8 in the META Tag has no effect.

Edit:

Now I solved it through making a list of all common UTF8/ANSI distortions (more than 1300) and wrote a function replacing all wrong character combinations with the right character. It works fine :-) .

1

There are 1 answers

12
Dan Korn On

I think the encoding of the "distorted string" in your question got munged further by posting it here. But a quick Google search for "javascript convert from utf-8" returns this blog post as the top hit: http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html

So it turns out that encoding and decoding UTF-8 in JavaScript is really easy. This works great for me:

var original = "Äußerungen üben";
var utf8 = unescape(encodeURIComponent(original));
//return utf8; // something like "ÃuÃerungen üben"
var output = decodeURIComponent(escape(utf8));
return output;