tonyp
tonyp

Reputation: 31

python coding of user input of hex numerals

How do I get the A to F hex numerals in as user input via something like:

hexnum=raw_input("input your hex number -> ")

Then I have a problem with such input not being convertible via:

number=int(hexnum)

I am looking for a simple example with a basic explanation.

Upvotes: 2

Views: 12325

Answers (2)

rob mayoff
rob mayoff

Reputation: 385600

The int constructor takes a base as its optional second argument.

>>> int('12AF', 16)
4783

Upvotes: 5

Raymond Hettinger
Raymond Hettinger

Reputation: 226296

This should work fine. Just inform int that it is working with base 16: int(hexnum, 16).

>>> hexnum = raw_input("input your hex number -> ")
input your hex number -> 2F
>>> print int(hexnum, 16)
47

Upvotes: 4

Related Questions