Search code examples
flutterdartencodingasciicodepages

Dart/Flutter - Cannot get encoding by name


I need to use Code Page 850 Ascii encoding for an issue I have, but not sure how to access/use it in Dart.

Ultimately, I need to convert a hexadecimal string to ascii. The hexadecimal includes special characters that aren't in generic ascii. Which is why I need cp850.

I've tried to access this encoding by calling the Encoding.getByName() function

// returns null
Encoding? encoding = Encoding.getByName('ibm850');

ibm850 appears to be the right name for cp850, based on the character sets accepted for this function.

Any help on this would be greatly appreciated!


Solution

  • You can use the package charset to encode/decode from/to various different character sets. E.g. you can do the following to decode some bytes encoded in cp850 by doing the following:

    import 'package:charset/charset.dart' as charset;
    
    void main() {
      final cp850EncodedMessage = [
        82, 155, 100, 32, 103, 114, 155, 100, 32, 109, 101, //
        100, 32, 102, 108, 155, 100, 101, 32, 112, 134, 33, //
      ];
    
      print(charset.cp850.decode(cp850EncodedMessage));
      // Rød grød med fløde på!
    }