How to print unicode of character in dart

Published by admin on

Print the character Unicode in Dart :

Dart string is a sequence of characters. Each character is represented as UTF-16 in dart. We can print the UTF-16 code unit or the Unicode of each character. Dart string has all these properties defined in it. In this tutorial, I will show you these properties and method with different examples :

 

codeUnits and codeUnitAt :

codeUnits is an inbuilt property of dart string. It returns one list of UTF-16 code units of the characters of the string. This list is immutable.

codeUnitAt is a method defined in dart string. It takes the index of a character and returns the code unit for that character.

 

Example program :

main(List<string> args) {
  var str = "abcd😄👻🤡🤚";
  print(str.codeUnits);
}

Output :

[97, 98, 99, 100, 55357, 56836, 55357, 56443, 55358, 56609, 55358, 56602]

Dart codeunits example

Similarly, you can use codeUnitAt method to get the UTF-16 value for a character.

 

runes :

This is a dart string property. It returns one iterable of the Unicode of all characters.

main(List<string> args) {
  var str = "abcd😄👻🤡🤚";
  print(str.runes.toList());
}

Output :

[97, 98, 99, 100, 128516, 128123, 129313, 129306]

Dart runes example

 

Convert the Unicode to character :

Find out the hexadecimal values of all Unicode :

97: 61
98: 62
99: 63
100: 64
128516 : 1f604
128123 : 1f47b
129313: 1f921
129306 : 1f91a

Use \uXXXX for four digits and use one {} for more than four digits.

main(List<string> args) {
  var str = "\u0061\u0062\u0063\u0064\u{1f604}\u{1f47b}\u{1f921}\u{1f91a}";
  print(str);
}

It will print back the above string :

abcd😄👻🤡🤚
Categories: dart

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *