Reputation: 79
I have the byte representation of UTF8, e.g.
195, 156 for "Ü" (capital U Umlaut)
I need to create a string for display in JavaScript out of these numbers - everything I tried failed. No methode I found recognizes "195" as a UTF leading byte but gave mit "Ã".
So how do I get a string to display from my stream of UTF8 bytes?
Upvotes: 0
Views: 575
Reputation: 501
You're working with decimal representations of the single byte components of the characters. For the example given, you have 195, 156. First, you have to converting to base 16's C3, 9C. From there you can use javascript's decodeURIComponent function.
console.log(decodeURIComponent(`%${(195).toString(16)}%${(156).toString(16)}`));
If you're doing this with a lot of characters, you probably want to find a library that implements string encoding / decoding. For example, node's Buffer objects do this internally.
Upvotes: 1