将字符串视为十六进制Python [英] Treat String as Hex Python

查看:140
本文介绍了将字符串视为十六进制Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将十六进制数的字符串视为十六进制数?例如,我正在加载一个十六进制数的文件,但是Python正在以字符串形式加载该文件.是否可以让Python将文件中的十六进制数视为十六进制数?我正在使用Python 2.7

How can I treat a string which is a hexadecimal number as a hexadecimal number? For example, I am loading in a file of hexadecimal numbers but Python is loading the file in as a string. Is it possible to get Python to treat the hexadecimal numbers in the file as hexadecimal numbers? I'm using Python 2.7

谢谢!

推荐答案

使用int,指定基数为16:

Use int, specifying base-16:

>>> int('FFABCD', 16)
16755661

编辑以回复评论: 好,误会了.

>>> hex(int('0061', 16))
'0x61'

我想是可行的,但是如果有人以更简单的方式回应,我不会感到惊讶.

Works I suppose, but I won't be surprised if someone responds with a simpler way.

这篇关于将字符串视为十六进制Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆