2

Encoding and decoding a string is not as easy as I thought.

The original string is as follows:

at the end of → al término de • después de

After PHP base64 encoding (used three times) it looks different:

VUVkSksxbFlVV2RrUjJoc1NVZFdkVnBEUW5aYWFuZDJXV28wWnpSdllWTkpSMFp6U1VoVVJIRllTblJoVnpWMlNVZFNiRWxQUzBGdmFVSnJXbGhPZDJSalQzQmplVUpyV2xSNGQxQm5QVDA9

When trying JS window.atob() to decode the string, the result is this:

at the end of â al término de ⢠después de

UTF-8 characters are not displayed properly. What function should I use to fix this?

  • please, take a look at this [conversation](https://stackoverflow.com/questions/30106476/using-javascripts-atob-to-decode-base64-doesnt-properly-decode-utf-8-strings), I guess your question is duplicated. – Ênio Abrantes Aug 01 '18 at 23:28
  • Why are you base64-encoding something 3 times anyway? – Brad Aug 01 '18 at 23:31

2 Answers2

4

Try

let decodedString = decodeURIComponent(escape(window.atob(yourString)))
Turan
  • 302
  • 2
  • 9
0

What happens with this is that JavaScript does not allow characters that are outside the range of Latin1, to be able to execute it you could do the following:

let str = 'at the end of → at the end of • after';
btoa(unescape(encodeURIComponent(str)))
// output: YXQgdGhlIGVuZCBvZiDihpIgYXQgdGhlIGVuZCBvZiDigKIgYWZ0ZXI=

This code will convert (from javascript) the string correctly to base64 and from PHP you can decode it well, now the problem is that JavaScript will not be able to decode it since it interprets it as the syntax is wrong (although it has been encoded) and so It will not work for you either.

Joel Jaime
  • 459
  • 2
  • 9