Async and Practical
UTF-8 Decode
Bytes to String
utf8.decode from dart:convert reads a List<int> of byte values and returns the decoded String. For ASCII characters the byte count equals the character count; multi-byte UTF-8 sequences would change that ratio.
Program
Play the program to turn five ASCII bytes into the word Hello.
utf8_decode.dart
import 'dart:convert';
void main() {
var bytes = [72, 101, 108, 108, 111];
var text = utf8.decode(bytes);
var size = bytes.length;
print('text="$text" bytes=$size');
}
utf8 codec
`utf8` from `dart:convert` is the encoder/decoder for UTF-8 byte sequences.
utf8.decode
`utf8.decode(bytes)` turns a `List<int>` of byte values into a Dart `String`.
byte vs char
Each ASCII byte maps to one character, so `bytes.length == text.length` here; multi-byte UTF-8 would make these differ.