搜索插件、代码仓与 APP 分类

插件详情v2.5.1

charset_converter

使用底层平台的字符集/编码转换器 - 无外部依赖

AndroidiOSWindowsLinux
近 30 天下载
3.8万
喜欢数
65
Pub 分数
160

安装插件

flutter pub add charset_converter

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个轻量级的 Flutter 包,用于在不同字符编码之间转换文本,利用底层平台的原生编码支持。它无需外部依赖,确保在 Android、iOS、Windows 和 Linux 等平台上高效且可靠地进行转换。适用于处理应用程序中的遗留或非 UTF-8 文本数据,尤其适合处理国际化内容或多种文件格式的应用。

适合场景

  • 将旧版文本文件从 ISO-8859-1 转换为 UTF-8
  • 处理具有未知编码的网页抓取输出
  • 在应用程序中处理多语言用户输入
  • 使用非标准编码读取配置文件

主要特点

  • 使用原生平台编码 API
  • 无外部依赖
  • 跨平台兼容性
  • 简单的同步 API
  • 支持常见字符集

适合对象

  • 处理国际文本的应用程序
  • 数据迁移工具
  • 文件格式解析器
  • 旧系统集成

使用建议

  • 依赖于平台特定的编码实现
  • 受操作系统支持的编码限制
  • 无内置回退机制
  • 不适合复杂的编码检测

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Charset converter

pub package Checks status

Encode and decode charsets using platform built-in converter. This saves app package size as you don't need any external charset maps or whole libraries like iconv. This package doesn't even contain any Dart dependencies. However this comes with the dependency on the platform.

This package was originally made to help deal with this StackOverflow question and Dart's lack of support for many specific charset like GBK, Big5, Windows-125X or ISO-8859-XX.

Usage

Checkout these snippets or full example.

Encoding
Uint8List encoded = await CharsetConverter.encode("windows1250", "Polish has óśćł");
Decoding
String decoded = await CharsetConverter.decode("windows1250",
    Uint8List.fromList([0x43, 0x7A, 0x65, 0x9C, 0xE6])); // Hello (Cześć) in Polish
Getting list of available charsets

Helpful if you don't know exact name of the charset.

List<String> charsets = await CharsetConverter.availableCharsets();

Warning: Please note that even if charset is not present on the list, it still might work. This is because the function is not returning the full list of aliases, this is presented in the example of iOS - TIS620 does not appear on the list, but ISO 8859-11, which is actually an alias of TIS620 does. To check if charset is available use CharsetConverter.checkAvailability.

Please also note that names are platform specific and may be different.

Checking availability of charset
bool isAvailable = await CharsetConverter.checkAvailability("EUC-KR");

Preview of what encoding you may find

This can be helpful if you are not sure what you are looking for.

How does it work?

Android comes with Java runtime, which has a built-in Charset class which has convenient encode and decode methods. All it's left to do is use channels to pass the data.

iOS can also work with charsets with CoreFoundation CFString functions fe. CFStringConvertIANACharSetNameToEncoding. We can also easily encode and decode Strings with init and data. It is a little bit cumbersome since we have to convert the values and use ported C API.

Windows can use MultiByteToWideChar and WideCharToMultiByte from Win32.

Linux can use libIconv in GLib. Since Flutter for Linux use GTK, it is pretty suitable. However, it accept a NUL terminated UTF-8 string (like C string end with\0), so in Dart side, String will be dealt by transferring into Uint8List, and add 0 at the end.

Contributing

Feel free to create issues, fix bugs, add new functionalities even if you are not sure about. Everyone is welcome!

See also

所属分类

源码仓库

使用此插件的项目