Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get bytes of a String in Dart?

How can I read the bytes of a String in dart? in Java it is possible through the String method getBytes().

See example

like image 438
gyorgio88 Avatar asked Feb 23 '19 17:02

gyorgio88


People also ask

How do you find the bytes of a string?

Java String getBytes() The Java String getBytes() method encodes the string into a sequence of bytes and stores it in a byte array. Here, string is an object of the String class. The getBytes() method returns a byte array.


1 Answers

import 'dart:convert';

String foo = 'Hello world';
List<int> bytes = utf8.encode(foo);
print(bytes);

Output: [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]

Also, if you want to convert back:

String bar = utf8.decode(bytes);
like image 68
Miguel Ruivo Avatar answered Sep 19 '22 20:09

Miguel Ruivo