¿Cómo convertir hexadecimal a decimal en Python? [duplicar]
Esta pregunta ya tiene una respuesta aquí:
- Convertir cadena hexadecimal a int en Python 9 respuestas
Tengo algún código Perl donde la función hex() convierte los datos hexadecimales a decimales. ¿Cómo puedo hacerlo en Python?
84
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
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
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
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
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