转换DART中的扩展ASCII字符

发布于 2025-01-29 01:43:11 字数 101 浏览 4 评论 0 原文

我的Flutter应用程序通过REST接口检索信息,该界面可以包含扩展的ASCII字符,例如e-Eacute 0xe9。如何将其转换为UTF-8(例如0xc3 0xa9),以使其正确显示?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜唯美灬不弃 2025-02-05 01:43:11

0xe9对应于ISO-8859/Latin 1编码中的e-atute(é)。 (这是“扩展ASCII”的许多可能的编码之一,尽管我个人将“ Extended ascii”一词与。)

您可以使用 String (它内部存储UTF-16) dart-convert/latin1codec-class.html“ rel =“ nofollow noreferrer”> latin1codec 。如果您真的想要UTF-8,则可以用

import 'dart:convert';

void main() {
  var s = latin1.decode([0xE9]);
  print(s); // Prints: é
  var utf8Bytes = utf8.encode(s);
  print(utf8Bytes); // Prints: [195, 169]
}

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]
}
九八野马 2025-02-05 01:43:11

我感到困惑,因为有时数据包含扩展的ASCII字符,有时包含UTF-8字符。当我尝试进行UTF-8解码时,它在扩展的ASCII上进行了baulk。
我通过尝试UTF8解码并在扩展ASCII时捕获错误来修复它,它似乎可以解码此确定。

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.

暗喜 2025-02-05 01:43:11

对于混合字符串,您可以使用

Uri.decodeFull('string%3Dwith%3Dmixed%3Dcontent');

Credits https://stackoverflow.com/a/a/a/73512147/7198006

For mixed strings, you can use

Uri.decodeFull('string%3Dwith%3Dmixed%3Dcontent');

credits https://stackoverflow.com/a/73512147/7198006

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文