Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove 0x from Hex String [duplicate]

Tags:

python

val1 = hex(100)
val2 = hex(50)
val3 = hex(int(val1, 16) - int(val2, 16))

hex_str = str(val1) + str(val2) + str(val3)

print(hex_str)

In the above example, my end goal is to have a string that looks like this: 643232

Currently however the result I am getting has the string as 0x640x320x32

Is there a way in Python to remove the 0x from the beginning of a hex string?

Or will I have to remove it using a regular expression with something like re.sub()?

EDIT: I also need to make sure I am always getting too characters. So for example if I was looking for the hex value of 5 I'd get 05

like image 520
Skitzafreak Avatar asked Nov 19 '22 14:11

Skitzafreak


1 Answers

try:

val1 = hex(100)[2:]
val2 = hex(50)[2:]
val3 = hex(int(val1, 16) - int(val2, 16))[2:]

hex_str = str(val1) + str(val2) + str(val3)

print(hex_str)
like image 186
Md. Rezwanul Haque Avatar answered Jan 11 '23 23:01

Md. Rezwanul Haque