data representations #12234
Replies: 5 comments 2 replies
-
If you started with the numbers 129 and 20, you can use arithmetic to get to 5249 as well by |
Beta Was this translation helpful? Give feedback.
-
Just to clarify, this is standard Python behaviour, not MicroPython-specific.
This is equivalent to x = struct.pack('BB', 129, 20) # x = b'\x81\x14'
a, b = struct.unpack('BB', x) # a = 129, b = 20 but you're working with bytes, so you could also just use x = struct.pack('BB', 129, 20) # x = b'\x81\x14'
a = x[0] # a = 129
b = x[1] # b = 20 |
Beta Was this translation helpful? Give feedback.
-
>>> x = 5249
>>> divmod(x, 256)
(20, 129)
>>> |
Beta Was this translation helpful? Give feedback.
-
Thnx muchly, always possible to get back to the starting point if you're a competent enough programmer. |
Beta Was this translation helpful? Give feedback.
-
Another - arguably more self-evident - approach is to use a bytearray: a = bytearray(2) # Store 2 values
a[0] = 20
a[1] = 129
print(a[0], a[1]) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
My tenuous grasp of upythons data formats & their representations is causing me grief again. Specifically the lack of symmetry when changing between formats keeps catching me out.
At the moment I can put 2 x 8 bit analog values into a pair of bytes
I can get the integer value
but when I try to backtrack to those original 2 values of 129 & 20
I fall at the first hurdle with an '0x1481 representation instead of b'\x81\x14'
Can someone show me how to get from 5249 back to the 129 & 20 values I started with?
Beta Was this translation helpful? Give feedback.
All reactions