Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How transform a dart's ByteData into a String?

I am reading a binary file and want to transform it into a String. How do I do it in Dart?

like image 241
Daniel Oliveira Avatar asked Jan 17 '19 21:01

Daniel Oliveira


People also ask

What is Uint8 list?

Uint8List is a list of integers where the values in the list are only 8 bits each, or one byte. The U of Uint8List means unsigned, so the values range from 0 to 255 . That's perfect for representing binary data!

How do you convert int to byte in darts?

The easiest approach would be to create a byte list ( Uint8list ), then view the underlying buffer as a 32-bit integer list ( Int32List ) and store the integer there. That will allow you to read back the bytes.

What is Dart Uint8List?

A fixed-length list of 8-bit unsigned integers. For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.


1 Answers

Try the following

String getStringFromBytes(ByteData data) {
  final buffer = data.buffer;
  var list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
  return utf8.decode(list);
}

Also see this answer.

like image 109
Oswin Noetzelmann Avatar answered Sep 30 '22 06:09

Oswin Noetzelmann