Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bytearray questions
#1
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
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Value error when converting hex value to bytearray shubhamjainj 7 10,331 Mar-20-2023, 05:30 PM
Last Post: Skaperen
  appending to a bytearray Skaperen 21 13,350 Mar-19-2023, 11:05 PM
Last Post: Skaperen
  Split Bytearray into separate Files by Hex delimter lastyle 5 2,491 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  Save multiple Parts of Bytearray to File ? lastyle 1 908 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  bytearray object - why converting to ascii? Chepilo 2 1,534 Nov-21-2022, 07:25 PM
Last Post: Chepilo
  Bytearray substitution Vismuto 1 2,565 Apr-14-2020, 09:18 AM
Last Post: TomToad
  int() function with bytearray() Jona66 1 2,365 Sep-08-2019, 12:41 PM
Last Post: ichabod801
  Conversion needed from bytearray to Floating point braveYug 1 4,064 May-07-2018, 12:23 PM
Last Post: snippsat
  Windows DIB format in bytearray to image? dusca 2 2,718 Mar-28-2018, 10:35 PM
Last Post: dusca
  ByteArray outside while true Schampbakken 5 4,326 Feb-18-2018, 02:02 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020