1

I have many strings where the accents are converted wrongfully. I take those Strings from an API, so I cannot get them in other encoding formats. As an example, the string é returns as é from the API. Is there any way I can convert these strings to show the accents correctly?

4
  • 1
    How do you make the call to this web api? If you e.g. are using dart:io you can just get the raw data and parse it manually with the correct charset. Commented May 4, 2020 at 16:43
  • 1
    Also, I am not sure your example are correct. é in UTF-8 translates into C3A9 which in latin1 would be é and not ã© (E3A9). In fact, if you try parse ã© as UTF-8, it will just be parsed as invalid UTF-8. Commented May 4, 2020 at 16:56
  • Sorry, I inserted the ã© manually and forgot to capitalize the a. I'll edit it. Commented May 4, 2020 at 18:04
  • @julemand101 I'm using the http/http.dart package to make the API call. Commented May 4, 2020 at 18:06

1 Answer 1

6

Well, you can try something like this:

import 'dart:convert';

void main() {
  const input = 'é';
  final output = utf8.decode(latin1.encode(input), allowMalformed: true);
  print(output); // é
}

Alternative you can get the response from your web call as bytes by using bodyBytes on the response object: https://pub.dev/documentation/http/latest/http/Response/bodyBytes.html

And parse it with: latin1.decode or whatever charset the server are sending the the data as.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.