Bytearray questions - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Bytearray questions (/thread-9166.html) |
Bytearray questions - mattps - Mar-24-2018 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*f9as 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 RE: Bytearray questions - DeaD_EyE - Mar-24-2018 import binascii binascii.b2a_hex(bytearray(b'K\x01\x01\x00\x00\x08\x04\x97*f9')) 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. RE: Bytearray questions - mattps - Mar-25-2018 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: 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. 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 |