从2字节的十六进制中获取十进制数值。

huangapple go评论72阅读模式
英文:

Obtaining decimal from 2-Byte hex

问题

我有一个问题,我们得到了以2字节形式表示的气压(Hg/1000)。这些数据来自串行读取,我们有以下有关数据的信息:

  • 8个数据位
  • 1个起始位
  • 1个停止位
  • 无奇偶校验

我正在尝试在Python中将这些字节转换为有效的气压读数(在20到32.5之间),例如以下示例数据:

1. ['0xf0', '0x73']
2. ['0xef', '0x73']
3. ['0xf1', '0x73']
4. ['0xf4', '0x73']
5. ['0xee', '0x73']
6. ['0xec', '0x73']

到目前为止,我已经能够得到第6个数据的值为351,或者将它们转换为十进制并相加得到236,115,尽管我不太确定接下来该怎么做。我相信这应该对应大约29.67Hg,但我不确定。

英文:

I have a problem where we are given the barometric pressure (Hg/1000) as 2 Bytes. The data is from a serial readout and we are provided with the following information regarding that:

  • 8 data bits
  • 1 Start bit
  • 1 Stop bits
  • No Parity

I am trying to convert the bytes into valid pressure readings (between 20 and 32.5) in python, from the following example data:

1. ['0xf0', '0x73']
2. ['0xef', '0x73']
3. ['0xf1', '0x73']
4. ['0xf4', '0x73']
5. ['0xee', '0x73']
6. ['0xec', '0x73']

So far I have been able to get the value 351 for number 6 or 236,115 by converting to decimal and adding them although I'm not really sure where to go from here. I believe this is supposed to correlate to around 29.67Hg but I am unsure.

答案1

得分: 2

数据看起来是以“小端”格式打包为int16值。

您可以使用int.from_bytesstruct.unpack来解压这些值。

根据您的预期值,从转换后的输出中需要除以1000,以将小数点移动到正确的位置。

由于输入数据中的值是字符串,您首先需要将它们转换为整数。由于它们表示十六进制值,可以使用int('0xce', 16)来完成此操作。

有多种方法可以做到这一点。以下是一个示例:

data = ['0xf0', '0x73'],
        ['0xef', '0x73'],
        ['0xf1', '0x73'],
        ['0xf4', '0x73'],
        ['0xee', '0x73'],
        ['0xec', '0x73']]
for idx, reading in enumerate(data):
    value = int.from_bytes([int(x, 16) for x in reading], 'little') / 1000
    print(f"{idx + 1}. {reading} is {value:0.03f}")

这将产生以下输出:

1. ['0xf0', '0x73'] is 29.680
2. ['0xef', '0x73'] is 29.679
3. ['0xf1', '0x73'] is 29.681
4. ['0xf4', '0x73'] is 29.684
5. ['0xee', '0x73'] is 29.678
6. ['0xec', '0x73'] is 29.676
英文:

The data looks like it packed as int16 values in "little endian" format.

You can unpack these values using int.from_bytes or struct.unpack

Looking at the value you expect, the output from the conversion needs to be divided by 1000 to move the decimal place to be in the correct location.

As the values are strings in the input data, you will need to turn them in to integers first. As they are representing hex values this can be done with int('0xce', 16).

There are lots of ways you could do this. Below is one example:

data = [['0xf0', '0x73'],
        ['0xef', '0x73'],
        ['0xf1', '0x73'],
        ['0xf4', '0x73'],
        ['0xee', '0x73'],
        ['0xec', '0x73']]
for idx, reading in enumerate(data):
    value = int.from_bytes([int(x, 16) for x in reading], 'little') / 1000
    print(f"{idx + 1}. {reading} is {value:0.03f}")

Which gives the following output:

1. ['0xf0', '0x73'] is 29.680
2. ['0xef', '0x73'] is 29.679
3. ['0xf1', '0x73'] is 29.681
4. ['0xf4', '0x73'] is 29.684
5. ['0xee', '0x73'] is 29.678
6. ['0xec', '0x73'] is 29.676

huangapple
  • 本文由 发表于 2023年3月7日 10:59:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定