Bottom Page

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
Quote
#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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Bytearray substitution Vismuto 1 107 Apr-14-2020, 09:18 AM
Last Post: TomToad
  int() function with bytearray() Jona66 1 291 Sep-08-2019, 12:41 PM
Last Post: ichabod801
  Value error when converting hex value to bytearray shubhamjainj 3 1,073 Mar-01-2019, 08:29 AM
Last Post: DeaD_EyE
  Conversion needed from bytearray to Floating point braveYug 1 799 May-07-2018, 12:23 PM
Last Post: snippsat
  Windows DIB format in bytearray to image? dusca 2 829 Mar-28-2018, 10:35 PM
Last Post: dusca
  ByteArray outside while true Schampbakken 5 1,555 Feb-18-2018, 02:02 PM
Last Post: buran
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 3,276 Nov-25-2017, 06:21 AM
Last Post: heiner55
  readinto a bytearray slice bhowerter 3 3,571 Sep-23-2016, 10:04 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)