Convert <int> list to string in dart?
I read the file and got a list of integers. For most of my work, this is what I want to get, but in one case, I also need to convert the List<int>
part to String
.
To be more specific, I want to encode a string in UTF-8.
This is what I just tried:
var decoder = new Utf8Decoder(figures); print(decoder.decodeRest());
But all I get is a list of integers.
+6
3 answers
You can use the dart: convert library that provides the UTF-8 codec:
import 'dart:convert'; void main() { List<int> charCodes = [97, 98, 99, 100]; String result = UTF8.decode(charCodes); print(result); }
+5
As already mentioned, String.fromCharCodes(List<int> charCodes)
is most likely you are looking for if you want to convert Unicode characters to string. If, however, all you need to do is combine the list into a string, which you can use Strings.join(List<String> strings, String separator)
.
Strings.join([1,2,3,4].map((i) => i.toString()), ",")) // "1,2,3,4"
+4