Keshav
Keshav

Reputation: 1091

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

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'.

Upvotes: 29

Views: 37387

Answers (3)

live-love
live-love

Reputation: 52484

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 ;

Upvotes: 27

dmarquina
dmarquina

Reputation: 4076

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/

Upvotes: 12

diegoveloper
diegoveloper

Reputation: 103541

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

Upvotes: 56

Related Questions