type change bytearray(4) to FLOAT32 and back. #12629
-
I can't find a way to easily convert I need to stream values in FLOAT32 via a serial SPI channel and the recipient must restore the sequence of bytes again in FLOAT32. In practice, this is what is available in C through the “union” type, where members with different types share the same memory area. There is something similar in the UCTYPES module. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Use |
Beta Was this translation helpful? Give feedback.
-
>>> import uctypes
>>> data = bytearray(4)
>>> data
bytearray(b'\x00\x00\x00\x00')
>>> s = uctypes.struct(uctypes.addressof(data), {'f32': uctypes.FLOAT32})
>>> s.f32
0.0
>>> s.f32 = 1.4
>>> data
bytearray(b'33\xb3?') |
Beta Was this translation helpful? Give feedback.
uctypes
appears to work perfectly for this.