Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endianness unclear for byte arrays #68

Open
praxamarnix opened this issue Jun 29, 2023 · 0 comments
Open

Endianness unclear for byte arrays #68

praxamarnix opened this issue Jun 29, 2023 · 0 comments

Comments

@praxamarnix
Copy link

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)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant