Python string from Hex to Hex (with leading zeros)

I have a problem with hex conversion in Python.

I have a string that represents the hexadecimal number - "02" , and I want to convert it to 0x02 and append it to another hexadecimal number.

My code is:

 valToWrite1 = '\x3c' valToWrite2 = '02' 

I want to join these two values ​​so that my result is "\x3c\x02" . It is important to keep leading zeros.

+4
source share
1 answer

You need binascii.unhexlify() :

 >>> import binascii >>> binascii.unhexlify("02") '\x02' >>> '\x3c' + _ '<\x02' 
+3
source

Source: https://habr.com/ru/post/1495173/


All Articles