Dart将IEEE-11073 32位FLOAT转换为简单的double [英] Dart Convert IEEE-11073 32-bit FLOAT to a simple double

查看:80
本文介绍了Dart将IEEE-11073 32位FLOAT转换为简单的double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有太多处理这些低级字节和数字的经验,所以我来这里寻求帮助.我正在Flutter应用程序中连接蓝牙温度计,并且根据他们的文档,我得到了一系列格式化为数字的数字.我正在尝试将这些数字转换为普通温度的两倍,但不知道如何进行.这是公司给我的例子".但是,当温度计上的读数为98.5时,我得到的响应是[113,14,0,254]

I don't have much experience working with these low level bytes and numbers, so I've come here for help. I'm connecting to a bluetooth thermometer in my Flutter app, and I get an array of numbers formatted like this according to their documentation. I'm attempting to convert these numbers to a plain temperature double, but can't figure out how. This is the "example" the company gives me. However when I get a reading of 98.5 on the thermometer I get a response as an array of [113, 14, 0, 254]

感谢您的帮助!

推荐答案

IEEE-11073是医疗设备中常用的格式.引用的表中包含所有内容,供您解码数字,尽管起初可能很难解密.

IEEE-11073 is a commonly used format in medical devices. The table you quoted has everything in it for you to decode the numbers, though might be hard to decipher at first.

让我们以第一个示例为例: 0xFF00016C .这是一个32位数字,第一个字节是指数,最后三个字节是尾数.两者均以2s补码表示形式编码:

Let's take the first example you have: 0xFF00016C. This is a 32-bit number and the first byte is the exponent, and the last three bytes are the mantissa. Both are encoded in 2s complement representation:

  • 指数, 0xFF ,以2的补码表示数字 -1
  • Mantissa, 0x00016C ,以2的补码表示为 364
  • Exponent, 0xFF, in 2's complement this is the number -1
  • Mantissa, 0x00016C, in 2's complement this is the number 364

(如果您不太确定数字如何以2的补码编码,请作为一个单独的问题提出.)

(If you're not quite sure how numbers are encoded in 2's complement, please ask that as a separate question.)

我们下一步要做的是确保它不是表中指定的特殊"值.由于您拥有的指数不是 0 (它是 -1 ),因此我们知道您还可以.因此,不需要特殊处理.

The next thing we do is to make sure it's not a "special" value, as dictated in your table. Since the exponent you have is not 0 (it is -1), we know that you're OK. So, no special processing is needed.

由于该值不是特殊值,因此其数字值简单为:尾数* 10 ^指数.因此,如您的示例所示,我们有: 364 * 10 ^ -1 = 36.4 .

Since the value is not special, its numeric value is simply: mantissa * 10^exponent. So, we have: 364*10^-1 = 36.4, as your example shows.

您的第二个示例与此类似.指数为 0xFE ,即2补码中的数字 -2 .尾数为 0x000D97 ,十进制为 3479 .同样,指数不为0,因此不需要特殊处理.所以您有: 3479 * 10 ^ -2 = 34.79 .

Your second example is similar. The exponent is 0xFE, and that's the number -2 in 2's complement. The mantissa is 0x000D97, which is 3479 in decimal. Again, the exponent isn't 0, so no special processing is needed. So you have: 3479*10^-2 = 34.79.

您说 98.5 的值,得到的是字节数组 [113、14、0、254] .让我们看看是否可以理解这一点.您的以十六进制表示的字节数组是: [0x71、0x0E,0x00、0xFE] .我猜您会以反向"顺序收到这些字节,因此,作为32位十六进制,实际上是 0xFE000E71 .

You say for the 98.5 value, you get the byte-array [113, 14, 0, 254]. Let's see if we can make sense of that. Your byte array, written in hex is: [0x71, 0x0E, 0x00, 0xFE]. I'm guessing you receive these bytes in the "reverse" order, so as a 32-bit hexadecimal this is actually 0xFE000E71.

我们的操作类似:指数再次为 -2 ,因为 0xFE 是您使用8位以2的补码形式编写 -2 的方式.(请参见上文.)尾数为 0xE71 ,等于 3697 .因此,电话号码是 3697 * 10 ^ -2 = 36.97 .

We proceed similarly: Exponent is again -2, since 0xFE is how you write -2 in 2's complement using 8-bits. (See above.) Mantissa is 0xE71 which equals 3697. So, the number is 3697*10^-2 = 36.97.

您声称这实际上是 98.5 .我最好的猜测是您正在以华氏度阅读,而您的设备正在以Celcius报告.如果进行数学计算,您会发现 36.97C = 98.55F 足够接近.我不确定您是如何获得 98.5 数字的,但是对于这样的设备,此结果似乎在您可以预期的精度范围内.

You are claiming that this is actually 98.5. My best guess is that you are reading it in Fahrenheit, and your device is reporting in Celcius. If you do the math, you'll find that 36.97C = 98.55F, which is close enough. I'm not sure how you got the 98.5 number, but with devices like this, this outcome seems to be within the precision you can about expect.

希望这会有所帮助!

这篇关于Dart将IEEE-11073 32位FLOAT转换为简单的double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆