首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >浮点数据格式sign+exponent

浮点数据格式sign+exponent
EN

Stack Overflow用户
提问于 2017-07-18 22:13:46
回答 2查看 76关注 0票数 0

我正在从热量表接收UART上的数据,但是我需要一些帮助来理解我应该如何处理这些数据。我有文件,但这对我来说还不够,我几乎没有这种计算的经验。

也许有合适技能的人可以向我解释如何用我从文档中得到的更好的例子来完成这件事。

代码语言:javascript
复制
One value consists of the following bytes: 

[number of bytes][sign+exponent] (integer) 

(integer) is the register data value. The length of the integer value is 
specified by [number of bytes].   [sign+exponent] is an 8-bit value that 
specifies the sign of the data value and sign and value of the exponent. The 
meaning of the individual bits in the [sign+exponent] byte is shown below: 

代码语言:javascript
复制
Examples: 

-123.45 = 04h, C2h, 0h, 0h, 30h, 39h 

87654321*103  = 04h, 03h , 05h, 39h, 7Fh, B1h 

255*103  = 01h, 03h , FFh 

现在再举一个实际数据的例子。

这是我从有关这方面的文档中获得的信息。

这是我从热量表中得到的一些数据。

10 00 56 25 04 42 00 00 1B E4

所以在我的例子中,04是字节数,42是sign+exponent,00 1B E4是(整数)。但我不知道该如何计算才能得到实际价值。

有什么帮助吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-18 22:29:20

根据你的例子,你的数据似乎是大端的.下面是如何将这些字节分解为需要使用位移位和掩蔽的字段。

代码语言:javascript
复制
n = b[0]
SI = (b[1] & 0x80) >> 7
SE = (b[1] & 0x40) >> 6
exponent = b[1] & 0x3f
integer = 0
for i = 0 to n-1:
    integer = (integer << 8) + b[2+i]
票数 1
EN

Stack Overflow用户

发布于 2017-07-18 22:25:04

尾数的符号是通过掩蔽( MSb,byte & 80h != 0 => SI = -1)从Sign+exponent字节的byte & 80h != 0 => SI = -1中获得的。

指数的符号也是用byte & 40h != 0 => SE = -1得到的。

指数值为EXP = byte & 3Fh

尾数INT是由其他四个字节组成的二进制数,可以作为一个整数读取(但要注意印度)。

最后,计算SI * INT * pow(10, SE * EXP)

在您的示例中,SI = 1SE = -1EXP = 2INT = 7140,因此

代码语言:javascript
复制
1 * 7140 * pow(10, -1 * 2) = +71.4

解释如何有效地实现这一点,不在本答案的范围之内。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45177995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档