How to convert ascii value in integer to its character equivalent in flutter?

16,096

Solution 1

You don't need dart:convert, you can just use String.fromCharCode

 print(String.fromCharCode(i));

More info: https://api.dartlang.org/stable/2.0.0/dart-core/String/String.fromCharCode.html

Solution 2

In Dart, use these 2 functions to convert from int (byte) to String (char) and vice versa.

int value = ';'.codeUnitAt(0); //get unicode for semicolon

String char = String.fromCharCode(value); //get the semicolon string ;

Solution 3

This ia exactly what you need to generate your alphabet:

import 'dart:core';

void RandomString() {
  List<int> a = new List<int>.generate(26, (int index) => index + 65);
  String f = String.fromCharCodes(a);
  print(f);
}

void main() {
  RandomString();
}

Also You can copy, paste and test it here https://dartpad.dartlang.org/

Share:
16,096
Keshav
Author by

Keshav

Updated on June 05, 2022

Comments

  • Keshav
    Keshav almost 2 years

    I am new to flutter and I just want to display a list of alphabets in a for loop. I just want to know how can I convert the integer to ascii character. I searched for this and I found dart:convert library, but I don't know how to use it.

    I want something like -

    for(int i=65; i<=90; i++){
    print(ascii(i));    //ascii is not any method, its just to understand my question
    }
    

    It should print the letters from 'A' to 'Z'.

  • Ilya Iksent
    Ilya Iksent about 3 years
    Notice: This is not a random string, but an alphabet (ABCDEFGHIJKLMNOPQRSTUVWXYZ).