Python Forum

Full Version: Bytearray questions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am currently neck deep in my first Raspberry Pi/NFC project and need some advice with a Python bytearray. When reading the contents of a Classic Mifare 1K card (16 sectors, 4 blocks):

https://www.nxp.com/docs/en/application-note/AN1304.pdf

I am returning the following bytearray:

bytearray(b'K\x01\x01\x00\x00\x08\x04\x97*f9')
I am trying to retrieve the serial ID of the card, which I know is in sector 0, block 0 and is 97:2A:66:39. I suspect it is contained within the last byte of the returned byte array, but after reading the Python documentation I don't understand the
Quote:x97*f9
as I suspect this contains what I need. How can I extract the contents of this byte (or am I barking up the wrong tree?).

Any pointers to get me started would be really helpful.

Thanks in advance,
Matt
import binascii
binascii.b2a_hex(bytearray(b'K\x01\x01\x00\x00\x08\x04\x97*f9'))
Output:
b'4b010100000804972a6639'
The returned Value is represented by raw bytes.
To get the hexadecimal representation of bytes, use binascii.b2a_hex.
a == ASCII
b == Binary

Finally you have to slice the result, to get the ID.
Thanks for the suggestion DeaD_EyE,

I tried:

import binascii
readable_data = binascii.b2a_hex(bytearray(b'K\x01\x01\x00\x00\x08\x04\x97*f9'))
print(readable_data)
And unfortunately get the following error:

Error:
Traceback (most recent call last): File "nfc-test2.py", line 23, in status_check readable_data=binascii.b2a_hex(card_data) TypeError: a bytes-like object is required, not 'int'
I'd also like to know what the x97*f9 means in the bytearray and how that translates to "readable data", so can answer the question myself next time and not have to bug anyone. Wink

Scratch that! The last thing I did yesterday was to annotate out the the data being passed from the byte array and replace it with the frame length!

Switched it back and got the expected result!

Thanks again DeaD_EyE