¿Cómo convertir hexadecimal a decimal en Python? [duplicar]


Esta pregunta ya tiene una respuesta aquí:

Tengo algún código Perl donde la función hex() convierte los datos hexadecimales a decimales. ¿Cómo puedo hacerlo en Python?

Author: Peter Mortensen, 2012-02-09

3 answers

Si por "datos hexadecimales" te refieres a una cadena de la forma

s = "6a48f82d8e828ce82b82"

Puede usar

i = int(s, 16)

Para convertirlo en un entero y

str(i)

Para convertirlo en una cadena decimal.

 163
Author: Sven Marnach,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-09 12:08:54
>>> int("0xff", 16)
255

O

>>> int("FFFF", 16)
65535

Lea los documentos .

 33
Author: Tim Pietzcker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-09 12:08:39

Podrías usar una evaluación literal:

>>> ast.literal_eval('0xdeadbeef')
3735928559

O simplemente especifique la base como argumento para int:

>>> int('deadbeef', 16)
3735928559
 13
Author: wim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-09 12:09:26