The UTF-8 Encoding and Decoding is different in Java and Javascript

I was trying to encode a string in Java, sending it to Javascript on client side and then trying to decode it. Without doing that, you can’t achieve the desired results.
Here is what I got –

String before encoding it in Java – D:\Net\S\English\ALL HITS ! NO WORDS !\All That She Wants….Ace Of Base.mp3
.

String after encoding in Java (Using URLEncoder.encode(text,”UTF-8″) function) – D%3A%5CNet%5CS%5CEnglish%5CALL+HITS+%21+NO+WORDS+%21%5CAll+That+She+Wants….Ace+Of+Base.mp3
.

String before decoding in JavaScript – D%3A%5CNet%5CS%5CEnglish%5CALL+HITS+%21+NO+WORDS+%21%5CAll+That+She+Wants….Ace+Of+Base.mp3
.

String after decoding in JavaScript – D:\Net\S\English\ALL+HITS+!+NO+WORDS+!\All+That+She+Wants….Ace+Of+Base.mp3
So if you try decode a UTF-8 string encoded in Java, “+” sign still remains.

So here is what I did in order to decode the string properly –

I replaced all the “+” sign appearances in string using regular expression inside the replace function. Similar things happen with other characters like “&” etc.

The final JavaScript code is like this –

text = decodeURIComponent(text);
text = text.replace(/\+/g,” “);

Introducing new category – Programming/Coding for discussing similar problems!

Leave a Reply

Your email address will not be published. Required fields are marked *