0

Working with NFC with pyscard I received a hexstring like this "01 CB"

I need to convert it to something like that b'\x01\xCB'

--

I know it is a 2 octet big endian and should be equal to 459.

I basically want to run that after the conversion int.from_bytes(b'\x01\xCB', byteorder='big')

Any help is appreciated, thanks

1 Answer 1

1

If you need just integer value you might simply remove space and treat it just like hex-number i.e.:

string = "01 CB"
digits = string.replace(" ","")
value = int(digits,16)
print(value)

Output:

459

int's second (optional) argument is base - this can be any number from 2 to 36.

Sign up to request clarification or add additional context in comments.

2 Comments

Right what I was looking for thank you! How would you go on converting the other way around? hex(value) seems to be the right start but how would you go matching the format?
@Garronde: if I would have to deal with values from 0 to 65535 (inclusive) I would unleash awk-style formatting following way: value = 459; string = "%02X %02X" % (value//256, value%256); print(string) which result in output: 01 CB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.