-1

How can I convert a string x='0x67c31080115dDfeBa0474B3893b2caB1d567438f' into hex to be x=0x67c31080115dDfeBa0474B3893b2caB1d567438f ?

I want to use the same value of the hex but not in a string format

In other word, how can I treat x as a hex value not a string type ( without ' ' )

Thanks

sheemar
  • 467
  • 3
  • 13
  • 2
    Does this answer your question? [how to parse hex or decimal int in Python](https://stackoverflow.com/questions/604240/how-to-parse-hex-or-decimal-int-in-python) – Thomas Schillaci Mar 11 '20 at 12:49
  • What exactly are you dong that requires your number to be represented internally as a hex value? Python represents all numbers in base 10 format, but the numeric operations result in the same output regardless of if the number is octal, decimal, or hex. – James Mar 11 '20 at 12:52

2 Answers2

2

There is no such thing as "hex value". 0x67c31080115dDfeBa0474B3893b2caB1d567438f belongs to type int. You can convert your string to int just with:

x = int(x, 16)

If, however, you want to print it as hex, either use:

print(hex(x))

or

print("{:x}".format(x))

(the first one adds "0x" to the beginning of the string, the other one does not).

Błotosmętek
  • 12,717
  • 19
  • 29
1

You could do it using ast.literal_eval:

import ast

x='0x67c31080115dDfeBa0474B3893b2caB1d567438f'

hex_val = hex(ast.literal_eval(x))
print(hex_val)
SitiSchu
  • 1,361
  • 11
  • 20