Alex Borg
Alex Borg

Reputation: 141

Python: Why is 0x01 an integer?

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!

Upvotes: 0

Views: 637

Answers (2)

Rogith
Rogith

Reputation: 21

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)

Upvotes: 0

user3273429
user3273429

Reputation: 21

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

Upvotes: 1

Related Questions