How to convert a bytearray(1) value to a 0-255 (uctypes.UINT8).... #17980
Unanswered
ADenislebel
asked this question in
Using MicroPython
Replies: 1 comment 3 replies
-
data = bytearray((0x55,))
value = int(data[0]) |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I just about feel ashamed to ask but my C programmer brain just can't comprehend why this seemingly trivial
task is so convoluted in MicroPython v1.24.1 on 2024-11-29; Raspberry Pi Pico with RP2040.
The context: I'm reading a port byte from a io_expander that gets load into Buf = bytearray(1).
Then I need to get the value (0/1) of each io_bit 0-7 using the following:
bit_val = (value >> io_bit) & 1
My problem is how to convert the bytearray() Buf variable converted to an int value that will work in that
line of code. If I put a fixed 0b00110011 as the value in line it works fine.
I've tried value = int(value), it reports a syntax error something about wrong base value.
Also tried value = bin(value) it works but fails to value created fails in the bit_val = (value >> io_bit) & 1
I might be able to use uctypes import to do the work but when I did that in the passed
I couldn't even print any of the uctypes structure elements as it immediately crashed the interpreter
and only a reset/power down/up got me back.
Regards a loss C programmer...Denis
to a prompt.
Beta Was this translation helpful? Give feedback.
All reactions