Enuff
Enuff

Reputation: 516

Decode utf8 character on javascript

I have a badly configured third party service that outputs strings like this:

"SK Uni=C4=8Dov vs Prostejov"

I want to replace on the fly all the wrong characters it sends me, so my modules work with the correctly decoded string

I have found on this website (https://www.compart.com/en/unicode/U+010D) that the =C4=8D substring corresponds to the utf-8 character č

https://www.compart.com/en/unicode/U+010D

č
...
UTF-8 Encoding:     0xC4 0x8D
UTF-16 Encoding:    0x010D
UTF-32 Encoding:    0x0000010D
...

but I cannot find the way to decode it automatically.

I've tried with:

>> String.fromCodePoint(0xc48d)
"쒍"


>> String.fromCodePoint("0xc4 0x8d")
RangeError    

>> String.fromCharCode(0xc48d)
"쒍"

etc...

If I do it with the utf-16 code, String.fromCodePoint(0x010D) outputs the correct character.

How can I make it work with utf-8 instead of utf-16 codes?

Should I convert my string to utf16 achieve what I want? If so, How can I convert it?

Upvotes: 0

Views: 2559

Answers (1)

nwellnhof
nwellnhof

Reputation: 33658

Since the encoding is almost identical to percent escapes used in URLs, you can simply use:

decodeURIComponent("SK Uni=C4=8Dov vs Prostejov".replace(/=/g, "%"))

Upvotes: 1

Related Questions