String to ASCII or ASCII to String in dart

Hi Guys, In this dart article let’s checkout How to get ASCII code from a Character or String from a ASCII code in dart language (will also work in Flutter programming).

What is ASCII?

ASCII stands from American Standard Code from Information Interchange, ASCII is a encoding standard character that use 7 bit binary code basically to represent 128 possible character, which include characters like digits, letters, punctuation marks & control Code.

Eg:- A ASCII code for letter capital ‘A’ is 65 & for small ‘a’ = 97, Likewise ASCII code for digit 0 is 48.

ASCII Code Chart

Download ascii chart from commons.wikimedia.org

Getting String from ASCII code in dart

Example 1: ASCII to String in dart

void main() {
  var asciiCode = 98;

  String mString = String.fromCharCode(asciiCode);

  print(mString); // b

}

Example 2: ASCII to String in dart

void main() {
  List<int> asciiCodes = [
    45,
    102,
    100,
    99,
    98,
    101,
    111,
    67,
    88,
    128,
    99,
    103,
    109
  ];
  String message = '';
  for (int code in asciiCodes) {
    message += String.fromCharCode(code);
  }
  print(message);
}

Output

-fdcbeoCXcgm

Getting ASCII Code from String in dart

Example 1: String to ASCII in dart

void main() {   
  String mString2 = 'a';

  var ascii = mString2.codeUnitAt(0);
  print(ascii);  // 97

}

Example 2: String to ASCII in dart

void main() {
  String message = "This is Example to get ASCII code from String";
  List<int> asciiCodes = [];
  for (int i = 0; i < message.length; i++) {
    asciiCodes.add(message.codeUnitAt(i));
  }
  print(asciiCodes);
}

Output

[84, 104, 105, 115, 32, 105, 115, 32, 69, 120, 97, 109, 112, 108, 101, 32, 116, 111, 32, 103, 101, 116, 32, 65, 83, 67, 73, 73, 32, 99, 111, 100, 101, 32, 102, 114, 111, 109, 32, 83, 116, 114, 105, 110, 103]