简体   繁体   中英

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

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? Eg 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 :-) .

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM