搜尋套件、儲存庫與 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

所屬分類

原始碼儲存庫

使用此套件的專案