I am reading a binary file and want to transform it into a String. How do I do it in Dart?
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!
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With