Reputation: 21
My flutter app retrieves information via a REST interface which can contain Extended ASCII characters e.g. e-acute 0xe9. How can I convert this into UTF-8 (e.g 0xc3 0xa9) so that it displays correctly?
Upvotes: 0
Views: 1690
Reputation: 12345
For mixed strings, you can use
Uri.decodeFull('string%3Dwith%3Dmixed%3Dcontent');
credits https://stackoverflow.com/a/73512147/7198006
Upvotes: 0
Reputation: 21
I was getting confused because sometimes the data contained extended ascii characters and sometimes UTF-8 characters. When I tried doing a UTF-8 decode it baulked at the extended ascii. I fixed it by trying the utf8 decode and catching the error when it is extended ascii, it seems to decode this OK.
Upvotes: 0
Reputation: 90174
0xE9 corresponds to e-acute (é) in the ISO-8859/Latin 1 encoding. (It's one of many possible encodings for "extended ASCII", although personally I associate the term "extended ASCII" with code page 437.)
You can decode it to a Dart String
(which internally stores UTF-16) using Latin1Codec
. If you really want UTF-8, you can encode that String
to UTF-8 afterward with Utf8Codec
.
import 'dart:convert';
void main() {
var s = latin1.decode([0xE9]);
print(s); // Prints: é
var utf8Bytes = utf8.encode(s);
print(utf8Bytes); // Prints: [195, 169]
}
Upvotes: 0