This is an old copy of the Python FAQ. The information here may be outdated.

How do I specify hexadecimal and octal integers?

To specify an octal digit, precede the octal value with a zero. For example, to set the variable “a” to the octal value “10” (8 in decimal), type:

>>> a = 010
>>> a

Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero, and then a lower or uppercase “x”. Hexadecimal digits can be specified in lower or uppercase. For example, in the Python interpreter:

>>> a = 0xa5
>>> a
>>> b = 0XB2
>>> b

If you have the integer value in a string, you can use int with the base set to zero to convert using the above rules:

>>> int("10", 0)
>>> int("010", 0)
>>> int("0x10", 0)

CATEGORY: programming


A Django site. rendered by a django application. hosted by webfaction.