Home > Enterprise >  Python: Why is 0x01 an integer?
Python: Why is 0x01 an integer?

Time:11-23

The following:

print(type(0x01))

Returns:

<class 'int'>

Whereas, the following:

print(0x01)

Returns

1

Now let's say we have:

x = "0x01"

How do I convert x such that it returns 1 when printed?

Thank you!

CodePudding user response:

You would need to convert using base 16 as suggested by @Joran Beasley

x = "0x01"
print(x)
x = int(x, 16)
print(x)

Returns:

0x01
1

CodePudding user response:

Actually 0x01 is an hexadecimal format which is base16 so to convert it to decimal format you need to use int() method as shown below

x ="0x01"
x = int(x, 16)
print(x)
  • Related