I am working on a smartcard in python and using pyscard library. I have the following data:
WRITE_PREF = [0X80, 0X12, 0X00, 0X01, 0X12, 0X01, 0X00, 0X00, 0X20, 0X00, 0X00, 0X00]
I am receiving random data(of length 10) in string format. After conversion to hex, I get the array with hex values of the random string data.
Problem: The array of hex values has the values in string form.
Example: ['0x33','0x32'....]
When I append WRITE_PREF and this hex data, I get the array like this:
[128, 18, 0, 1, 18, 1, 0, 0, 32, 0, 0, 0, '0x33', '0x32']
I convert the first part to hex and replace the array values of integer with the hex ones. When I transmit data using the command:
card.connection.transmit(final_array_in_hex)
I get error. I am pretty sure it is because the values in array are hex strings. If I transmit some data manually like:
[0x33,0x32,0x30]
It is working fine. So the problem must be because of the quotes in array data. Example:
['0x33','0x32','0x30']
Is there a way to solve this?
You have a str
representing a hexadecimal number, let's turn that into an int
. We save space using int
, also int
is flexible. We can choose how to represent this int
.
s = ['0x33','0x32']
s = [int(_s, base=16) for _s in s]
print(s) # defaults to displaying an int as decimal
print(list(f'0x{_s:x}' for _s in s)) # specify that we want to display as `x` or hexamdecimal
Output:
[51, 50]
['0x33', '0x32']