8

I'm getting a Bluetooth Characteristic from a Bluetooth controller with flutter blue as a List. This characteristic contains weight measurement of a Bluetooth scale. Is there a function to convert this list of ints to a Double?

I tried to find some background on float representations by reading the IEEE 754 standard. There is the dart library typed_data but I am new to dart and have no experience with this lib.

Example:

I have this List: [191, 100, 29, 173] which is coming from a bluetooth controller as a IEEE754 representation for a float value. Now i believe i have to convert each int to hex and concat these values: bf 64 1d ad

Next thing need to do is convert this to double, but i cannot find a function to convert hex to double. Only int.parse("0xbf641dad").

nvano
  • 337
  • 6
  • 13
  • Can you give an example of what you have and what your want. I am not entirely sure about "Is there a function to convert this list of ints to a Float". I am reading this as you want to convert the list into a single Float value. But I need an example to be sure. – julemand101 Jul 31 '19 at 08:03
  • 1
    This basically answers my question: https://stackoverflow.com/questions/55355482/parsing-integer-bit-patterns-as-ieee-754-floats-in-dart – nvano Aug 01 '19 at 09:22

5 Answers5

6

I guess your mean to convert the list of ints to a list of floats, not to a single float, right? First, dart has no type called float. Instead it has type double. To convert it, you can use the map() function:

var listInt = [1, 2, 3];
var listDouble = list.map((i) => i.toDouble()).toList();
Tidder
  • 1,126
  • 1
  • 7
  • 15
5

Had this same issue.

I think you mean how do you make the four bytes into a float32 ? I needed to do the same thing.

you will want to do something like this:

first take the List value as a byte buffer, then take the byte data, then you can use the getFloat32 function.

ByteBuffer buffer = new Int8List.fromList(value_in).buffer;
ByteData byteData = new ByteData.view(buffer);
result = byteData.getFloat32(0);

Just be a little aware that the order of bytes from the Bluetooth may be back to front, as the convention varys.

Also you will need the typed_data library:

import 'dart:typed_data'; //for data formatting

I'm trying to go in the other direction now . . . for the obvious reason.

Ozzy
  • 51
  • 2
2

You may need this:

double parseHexString(String hexStr,  bool littleEndian){
  if(hexStr.length % 2 != 0){
    return 0;
  }
  if(littleEndian == true){
    List<int> bytes = hex.decode(hexStr).reversed.toList();
    hexStr = hex.encode(bytes);
  }
  var byteConvert = ByteData(12);
  byteConvert.setInt64(0, int.parse(hexStr,radix: 16));
  return byteConvert.getFloat64(0);}

And demo :

double lat = parseHexString("0000004069665E40",true);
//lat = 121.60017395019531
Joe Qian
  • 493
  • 6
  • 13
0

If you already have:

int.parse("0xbf641dad")

I don't see why this wouldn't work:

int.parse("0xbf641dad").toDouble();
AKushWarrior
  • 504
  • 3
  • 16
  • The hexadecimal bytes presumably represent the bytes of an IEEE-754 floating-point number, not the bytes of an integer value. – jamesdlin Jul 22 '22 at 17:56
0

I used dart:typed_data for this:

import 'dart:typed_data';

List<int> intList = [191, 100, 29, 173];
double asFloat = ByteData.view(Uint8List.fromList(List.from(intList)).buffer).getFloat32(0, Endian.little);

Library reference: https://api.dart.dev/be/136883/dart-typed_data/dart-typed_data-library.html