typed_data
typed_data copied to clipboard
Endianness unclear for byte arrays
Given the following example code:
import 'dart:typed_data';
void main(List<String> arguments) {
final a = Float32List.fromList([1.0, 2.0]);
final b = a.buffer.asUint8List();
print(b);
final c = ByteData(8);
c.setFloat32(0, 1.0);
c.setFloat32(4, 2.0);
print(c.buffer.asUint8List());
}
// Result:
// [0, 0, 128, 63, 0, 0, 0, 64]
// [63, 128, 0, 0, 64, 0, 0, 0]
The ByteData.setFloat32() function definitely contains a way to set the endianness to little endian or big endian. But Float32List conversion doesn't seem to have this functionality and on my computer it is stored as little endian.
I was hoping for a smart way to convert my doubles into a big endian array of data, but the only way I can think of now is to write a for loop with ByteData and use setFloat32 to copy the data.
Is there a way to simply write something like Float32List.fromList([1.0, 2.0]).buffer.asUint8List(Endian.big)?