1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Flutterで文字コードを変換する charset_converterの使い方

Posted at

【やりたい事】

Shift_JISからUTF-8へ変換。
他にも様々な文字コードに対応しています。

文字コードを変換するパッケージでcharset_converterが役立ったので記録として残します。
dart:convertライブラリはUTF-8といった一部のエンコーディングについてはサポートしていますが、Shift_JISについてはサポートしていません。

【環境】

Flutter 3.7.7
Dart 2.19.4

【パッケージ】

charset_converter: ^2.1.1

【対応コードの確認方法】

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

上記のコードで確認できます。
2023年8月4日時点は以下になります。

[macintosh, x-mac-japanese, x-mac-trad-chinese, x-mac-korean, x-mac-greek, x-mac-cyrillic, x-mac-thai, x-mac-simp-chinese, x-mac-centraleurroman, x-mac-symbol, x-mac-dingbats, x-mac-turkish, x-mac-croatian, x-mac-icelandic, x-mac-romanian, x-mac-ukrainian, utf-16, utf-7, utf-8, utf-32, utf-16be, utf-16le, utf-32be, utf-32le, iso-8859-1, iso-8859-2, iso-8859-3, iso-8859-4, iso-8859-5, iso-8859-6, iso-8859-7, iso-8859-8, iso-8859-9, iso-8859-10, iso-8859-11, iso-8859-13, iso-8859-14, iso-8859-15, cp437, cp737, cp775, cp850, cp851, cp852, cp855, cp857, cp860, cp861, cp862, cp863, cp864, cp865, cp866, cp869, cp874, cp932, cp936, cp949, cp950, windows-1252, windows-1250, windows-1251, windows-1253, windows-1254, windows-1255, windows-1256, windows-1257, windows-1258, us-ascii, gb18030, iso-2022-jp, iso-2022-jp-2, iso-2022-jp-1, iso-2022-cn, iso-2022-kr, euc-jp, gb2312, euc-kr, shift_jis, koi8-r, big5, x-mac-roman-latin1, hz-gb-2312, big5-hkscs, koi8-u, x-nextstep, ibm037]

【使用方法】

(例)
Shift-JISからUTF-8へ

final utf8Data = await CharsetConverter.decode('Shift_JIS', contents);

contentsの部分に変換したいものを入れれば変換してくれます。

【最後に】

文字コードの変換で困っている方のお力になれれば幸いです。今回紹介したパッケージは様々な文字コードに対応していますので、使ってみて下さい。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?