Vincent Guttmann
Vincent Guttmann

Reputation: 293

Dart convert two Uint8's to one Uint16

I am building an app to read data from a spectrophotometer via Bluetooth. The measurements are sent as two bytes meant to be converted to one unsigned 16-bit integer. Thanks to the library I am using for the Bluetooth communication, those two bytes are converted two a Uint8List.

I have looked around, but I have found no solution whatsoever. How would I do that here?

I don't care if it's pretty. Heck, if you would provide a solution that uses black magic, I would use it. The only thing it has to do is to do its job, even if it's slow. I only need to get out three Uint16's, and the acceptable timespan is about one second, so even the most inefficient solution will do here.

Upvotes: 4

Views: 2209

Answers (1)

fsw
fsw

Reputation: 3695

You can simply use binary shift which should be super efficient:

(list[0] << 8) + list[1]

Demo:

var list = new Uint8List(2);
list[0] = 1;
list[1] = 1;
print((list[0] << 8) + list[1]);

Upvotes: 6

Related Questions