Parsing integer bit-patterns as IEEE 754 floats in dart

孤者浪人 提交于 2019-12-24 18:44:57

问题


I am getting 4 bytes of data through an interface(Bluetooth, List). The data is representing IEEE 754 float (e.g. 0x3fd0a3d7, which represents approximately 1.63 as a binary32 float)

Is there a way in dart lang to convert / type-pun this to float and then double? Something like intBitsToFloat in Java. Couldn't find anything. Or do I just have to write the IEEE 754 parsing myself?


回答1:


This is working, just import the dart:typed_data library:

  var bdata = ByteData(4);
  bdata.setInt32(0, 0x3fd0a3d7);
  print(bdata.getFloat32(0)); //Prints: 1.6299999952316284

(I'm not sure this is the most reliable way)



来源:https://stackoverflow.com/questions/55355482/parsing-integer-bit-patterns-as-ieee-754-floats-in-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!