How to convert String/Number to Byte Array (Byte List) in dart/flutter?

How to convert string to List in Flutter (dart string to list int): This concise article shows you how to convert a string or a number (integer or double) to a byte array (a list of bytes) in Dart (and Flutter as well).

Converting a String into a Byte Array

There is more than one way to turn a given string into a byte array in Dart(Convert int to list in Dart).

Dart string to list int : Using the utf8.encode() method

The utf8.encode() method (which can be imported from dart:convert) takes a string and return a list of UTF-8 bytes.

Example:

// main.dart
import 'dart:convert';

void main() {
  String text = 'KindaCode.com';
  List<int> bytes = utf8.encode(text);
  print(bytes);
}

Output:

[75, 105, 110, 100, 97, 67, 111, 100, 101, 46, 99, 111, 109]

Using the codeUnits property of the String class

The codeUnits property of the String class returns a list of UTF-16 code units.

Example:

// main.dart
void main() {
  String text = 'KindaCode.com';
  List<int> bytes = text.codeUnits;
  print(bytes);
}
75, 105, 110, 100, 97, 67, 111, 100, 101, 46, 99, 111, 109]

Using the runes property of the String class

The runes property gives you an iterable of Unicode code points of a string.

Example:

// main.dart
void main() {
  String text = 'Welcome to KindaCode.com';
  Iterable<int> bytes = text.runes;

  // convert the iterable to a list
  List<int> list = bytes.toList();
  print(list);
}

Output:

[87, 101, 108, 99, 111, 109, 101, 32, 116, 111, 32, 75, 105, 110, 100, 97, 67, 111, 100, 101, 46, 99, 111, 109]

Converting a Number into a Byte Array

To convert a number to a byte array, you can first convert it to a string and then use one of the approaches mentioned in the previous section to get the job done.

Example:

// main.dart
import "dart:convert";

void main() {
  // integer to byte array
  int myInt = 2023;
  print(utf8.encode(myInt.toString()));

  // double to byte array
  double myDouble = 3.1416;
  print(utf8.encode(myDouble.toString()));
}

Output:

[50, 48, 50, 51]
[51, 46, 49, 52, 49, 54]

Flutter List<Object to String FAQs

How do you convert string to list int in Dart?

List lstring = [“1”, “2”]; List lint = lstring. map(int. parse). toList();

How do you split a string to a list in Dart?

In Dart splitting of a string can be done with the help split string function in the dart. It is a built-in function use to split the string into substring across a common character. This function splits the string into substring across the given pattern and then store them to a list.

What is parse() in Dart?

Contribute to Docs. In Dart, the . parse() method is used to convert a string representation of data into a specified data type.

Check Also : How to get Element at specific Index from List in Dart

Read more updates related to flutter at flutterfever.com.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More