简体   繁体   中英

Javascript decodeURICompnent() not workign with MySQL encoded UTF-8 String

I've tried using decodeURIComponent() and decodeURI() in javascript on the String "Завтра была война" , which my MySQL database encoded on entering from "Завтра была война"

When I enter this String on (some) decoding websites (like https://2cyr.com/decode/ or https://encoder.mattiasgeniar.be/ ) it decodes just fine, on others it doesn't work ( https://www.browserling.com/tools/utf8-decode ) and it also doesn't work with the aforementioned function javascript provides (seen here: https://www.w3schools.com/code/tryit.asp?filename=G6VG1L4MSBD8 ).

Why is that the case? Is this a different UTF-8 encoding than what decodeURI[Component]() is for? Can Javascript even decode my specific (already encoded) String or do I have to change the database?

As you can see in the tools that manage to "decode" this string, it looks like someone took a UTF-8 encoded string and treated the underlying bytes as if they were a Windows-1252 encoded string.

If you receive this from a server (which exact bytes do you receive by the way? Post the HTTP response with the headers) and you have control over the server, you should fix this server-side, instead of trying to work with this in JS.

decodeURIComponent works with strings like "%D0%97%D0%B0%D0%B2%D1%82%D1%80%D0%B0%20%D0%B1%D1%8B%D0%BB%D0%B0%20%D0%B2%D0%BE%D0%B9%D0%BD%D0%B0" .

Завтра была война is Mojibake for Завтра была война . That means that some step in the processing got confused between latin1 and utf8. The proper hex for the Cyrillic is (I added spaces)

D097 D0B0 D0B2 D182 D180 D0B0 20 D0B1 D18B D0BB D0B0 20 D0B2 D0BE D0B9 D0BD D0B0

That is what you should see with SELECT HEX(col) .. from MySQL.

You should probably not use any "decode" functions.

See this for likely causes of Mojibake.

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