Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 363
Vincent Guttmann Avatar asked Oct 25 '25 04:10

Vincent Guttmann


1 Answers

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]);
like image 176
fsw Avatar answered Oct 26 '25 19:10

fsw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!