Dart/Flutter - Convert ASCII Integer Codes to String and Vice Versa

Find out how to convert ASCII codes to text or vice versa in Dart.

ASCII (American Standard Code for Information Interchange) is one of the mostly used character encodings. It's supported by most computer systems for data exchange. ASCII has a length of 7-bit which and it contains 128 characters. (0-127). There is also an expansion for ASCII which contains 256 characters (0-255). Sometimes you may need to convert a text to ASCII codes or the other way around, convert ASCII codes to a text. This tutorial gives you examples of how to do it in Dart programming language, which also works in any Dart framework including Flutter.

Convert ASCII Integer Codes to Characters (String)

If you have an ASCII code represented as an integer, you can use String.fromCharCode factory method to convert it to a character. The method accepts an integer and returns a string.

  String.fromCharCode(int charCode)

Example:

  String char = String.fromCharCode(65);
  print('char: $char'); // char: A

If you have a list of ASCII codes stored in a list of integers, you can use String.fromCharCodes method. It accepts a list of integers and returns a string. It also has two non required positional arguments which can be passed after the list of ASCII codes. The start argument can be passed to set the first index (inclusive) of the values in the list to be included in the conversion, while the end argument can be passed to set the last index (exclusive) of the values in the list to be converted.

  String.fromCharCodes(Iterable<int> charCodes, [int start = 0, int? end])

Examples:

  String text = String.fromCharCodes([65, 66, 67, 68, 69]);
  print('text: $text'); // text: ABCDE
  String text2 = String.fromCharCodes([65, 66, 67, 68, 69], 1);
  print('text2: $text2'); // text2: BCDE
  String text3 = String.fromCharCodes([65, 66, 67, 68, 69], 1, 2);
  print('text3: $text3'); // text3: B

Convert Characters/String to ASCII Integer Codes

Characters or texts are usually represented as a string in Dart. To convert a character of a string at a certain index to an ASCII integer value, use String.codeUnitAt method.

  int codeUnitAt(int index);

Example:

  int codeUnit = 'A'.codeUnitAt(0);
  print('codeUnit: $codeUnit'); // codeUnit: 65

If you want to convert all characters of a string to ASCII integer values, just access the codeUnits property of the string.

  List<int> get codeUnits

Example:

  List<int> codeUnits = 'ABCDE'.codeUnits;
  print('codeUnits: $codeUnits'); // codeUnits: [65, 66, 67, 68, 69]

Be careful that codeUnitAt or codeUnits may return a value not within ASCII valid range if the character is not an ASCII character.

Summary

That's all for this tutorial. String.fromCharCode and String.fromCharCodes factory methods can be used to convert ASCII codes to a text, while String.codeUnitAt method and String.codeUnits properties can be used to convert a text to ASCII codes. You can also read about: