十六进制字符串变量到python中的十六进制值转换 [英] Hex string variable to hex value conversion in python

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

问题描述

我有一个变量调用hex_string.该值可以是"01234567".现在,我想从此变量获取一个十六进制值,而不是字符串类型,该值为0x01234567.此变量的值可能会更改.所以我需要一个通用的转换方法.

I have a variable call hex_string. The value could be '01234567'. Now I would like to get a hex value from this variable which is 0x01234567 instead of string type. The value of this variable may change. So I need a generic conversion method.

推荐答案

我认为您可能会混淆数字及其表示形式. 0x0123456719088743是完全相同的东西. "0x01234567""19088743"不是(请注意引号).

I think you might be mixing up numbers and their representations. 0x01234567 and 19088743 are the exact same thing. "0x01234567" and "19088743" are not (note the quotes).

要从十六进制字符串转换为整数,请使用> > .

To go from a string of hexadecimal characters, to an integer, use int(value, 16).

要从整数到以十六进制表示该数字的字符串,请使用 hex(value) .

To go from an integer, to a string that represents that number in hex, use hex(value).

>>> a = 0x01234567
>>> b = 19088743
>>> a == b
True
>>> hex(b)
'0x1234567'
>>> int('01234567', 16)
19088743
>>>

这篇关于十六进制字符串变量到python中的十六进制值转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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