Skip to content Skip to sidebar Skip to footer

Correct Interpretation Of Hex Byte, Convert It To Float

I am currently trying to connect to an electric meter via RS485. It works quite well so far, except that I have problems reading what the meter is writing back on the RS485 line. I

Solution 1:

You can use struct.unpack() to get the numbers from the data. Here is the way:

In [171]: importstruct

In [190]: b2
Out[190]: b'\x01\x03J8\x00\x02S\xde'

In [191]: struct.unpack('B'*len(b2), b2)
Out[191]: (1, 3, 74, 56, 0, 2, 83, 222)

In [192]: [*map(hex, struct.unpack('B'*len(b2), b2))]
Out[192]: ['0x1', '0x3', '0x4a', '0x38', '0x0', '0x2', '0x53', '0xde']

Solution 2:

C, e and / are bytes too; they are ASCII characters so don't need to be displayed with \x.. hex escapes.

You don't need to decode the \x.. hex escapes either, they are just how Python gives you the debugging representation of a bytes object; each byte is either displayed as an escape sequence or a printable ASCII letter.

The same happens to your something like this example:

>>> b'\x01\x03\x04\x43\x67\x81\xEC\x43\x68'b'\x01\x03\x04Cg\x81\xecCh'

That's the exact same value, but \x68 is the ASCII letter h, etc.

The output is the default repr() output for bytes objects. It can't be changed. You'll have to write your own code if you need different output.

You could display all bytes as hex with the binascii.hexlify() function, for example:

>>>import binascii>>>binascii.hexlify(b'\x01\x03\x04\x43\x67\x81\xEC\x43\x68')
b'010304436781ec4368'

Now you have a bytes string with hex characters. Or you could use a str.join() function with each individual byte converted to hex with a \x literal text prefixed:

>>>''.join(r'\x{:02x}'.format(byte) for byte inb'\x01\x03\x04\x43\x67\x81\xEC\x43\x68')

'\x01\x03\x04\x43\x67\x81\xec\x43\x68'

>>>print(''.join(r'\x{:02x}'.format(byte) for byte inb'\x01\x03\x04\x43\x67\x81\xEC\x43\x68'))
\x01\x03\x04\x43\x67\x81\xec\x43\x68

That's a str object with \, x and hex digits as characters.

Note that this is all just displaying the value. The value didn't change; the bytes are all there however you display them. If you need to convert 4 bytes to a float, then just convert those 4 bytes:

struct.unpack('f', yourbytes[4:])  # 4 bytes for C float in native order

This uses the struct module to use the bytes directly. No hexadecimal representation needed:

>>>import struct>>>struct.unpack('f', b'\x01\x03\x04Ce/\xec\xe2'[:4])
(132.01173400878906,)

Post a Comment for "Correct Interpretation Of Hex Byte, Convert It To Float"