Question

I have read a file and got myself 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 some of List<int> into a String.

To be more specified, I want to encode the 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.

Was it helpful?

Solution

String.fromCharCodes(List<int> charCodes) is probably what you're looking for.

  List<int> charCodes = const [97, 98, 99, 100];
  print(new String.fromCharCodes(charCodes));

OTHER TIPS

You can use the dart:convert library which provides an UTF-8 codec:

import 'dart:convert';

void main() {
  List<int> charCodes = [97, 98, 99, 100];
  String result = UTF8.decode(charCodes);
  print(result);
}

As mentioned String.fromCharCodes(List<int> charCodes) is likely what you are looking for if you want to convert unicode characters into a String. If however all you wanted was to merge the list into a string you can use Strings.join(List<String> strings, String separator).

Strings.join([1,2,3,4].map((i) => i.toString()), ","))  // "1,2,3,4"

You can use the dart:convert library which provides an UTF-8 codec but be sure to use utf8 all small to use its methods.

List<int> byteArray = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100];
String result = utf8.decode(byteArray);
List<int> orginal = utf8.encode(result);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top