mj.dev
mj.dev

Reputation: 23

How can I convert a utf8 to ISO-8859-1 in Dart?

I need to use iso-8859 type in flutter but the method iconv not supported, is there any other method ?

Upvotes: 1

Views: 2322

Answers (1)

lrn
lrn

Reputation: 71653

I'd convert it to a string and then convert the string to the ISO-8859-X code page that you want, which in this case is probably ISO-8859-6 Latin/Arabic. The convert package (now) supports all the ISO-8859 code pages.

import "dart:convert" show utf8;
import "package:convert" show latinArabic;
...
  var utf8Bytes = ...;
  var string = utf8.decode(utf8Bytes);
  var latinArabicBytes = latinArabic.encode(string);

You can create helper converter for that as:

  final utf8ToLatinArabicConverter = utf8.decoder.fuse(latinArabic.encoder);
  final utf8ToLatinARabic = utf8ToLatinArabicConverter.convert;
  ....
     var latinArabicBytes = utf8ToLatinArabic(utf8Bytes);

Upvotes: 5

Related Questions