[英]Python convert a string containing hex to actual hex
I have a hex string , but i need to convert it to actual hex . 我有一个十六进制字符串 ,但是我需要将其转换为实际的 十六进制 。 For example, i have this hex string :
例如,我有这个十六进制字符串 :
3f4800003f480000
One way I could achieve my goal is by using escape sequences: 我可以实现目标的一种方法是使用转义序列:
print("\x3f\x48\x00\x00\x3f\x48\x00\x00")
However, I can't do it this way, because I want create together my hex from multiple variables. 但是,我不能这样做,因为我想根据多个变量一起创建十六进制。
My program's purpose is to: 我的程序的目的是:
100
100
100
: 100 * 100 = 10000
100
: 100 * 100 = 10000
2710
2710
0000
0000
2710
again 2710
0000
once more 0000
Result I'm expecting is 2710000027100000
. 我期望的结果是
2710000027100000
。 Now I need to pass this hexadecimal number as argument to a function (as hexadecimal). 现在,我需要将此十六进制数字作为参数传递给函数(十六进制)。
In Python, there is no separate type as 'hex'. 在Python中,没有单独的类型为'hex'。 It represents the hexadecimal notation of the number as
str
. 它表示数字的十六进制表示形式为
str
。 You may check the type
yourself by calling it on hex()
as: 您可以通过在
hex()
上调用它来检查type
:
# v convert integer to hex
>>> type(hex(123))
<type 'str'>
But in order to represent the value as a hexadecimal, Python prepends the 0x
to the string which represents hexadecimal number. 但是为了将值表示为十六进制,Python在表示十六进制数字的字符串前加上了
0x
。 For example: 例如:
>>> hex(123)
'0x7b'
So, in your case in order to display your string as a hexadecimal equivalent, all you need is to prepend it with "0x" as: 因此,在您的情况下,为了将您的字符串显示为十六进制等效项,您所需要做的就是在其前面加上“ 0x”,如下所示:
>>> my_hex = "0x" + "3f4800003f480000"
This way if you probably want to later convert it into some other notation, let's say integer (which based on the nature of your problem statement, you'll definitely need) , all you need to do is call int
with base 16 as: 这样,如果您以后可能想将其转换为其他符号,就可以说整数(根据问题陈述的性质,您肯定会需要它) ,您要做的就是以16为基数调用
int
:
>>> int("0x3f4800003f480000", base=16)
4559894623774310400
In fact Python's interpreter is smart enough. 实际上,Python的解释器足够聪明。 If you won't even prepend "0x", it will take care of it.
如果您甚至不加“ 0x”,它都会照顾好它。 For example:
例如:
>>> int("3f4800003f480000", base=16)
4559894623774310400
"0x" is all about representing the string is hexadecimal string in case someone is looking/debugging your code (in future), they'll get the idea. “ 0x”仅代表十六进制字符串,以防万一有人在寻找/调试您的代码(以后),他们会明白的。 That's why it is preferred.
这就是为什么它是首选。
So my suggestion is to stick with Python's Hex styling, and don't convert it with escape characters as "\\x3f\\x48\\x00\\x00\\x3f\\x48\\x00\\x00" 因此,我的建议是坚持使用Python的十六进制样式,不要将其与转义字符转换为“ \\ x3f \\ x48 \\ x00 \\ x00 \\ x3f \\ x48 \\ x00 \\ x00”
From the Python's hex
document : 从Python的
hex
文档中 :
Convert an integer number to a lowercase hexadecimal string prefixed with “0x”.
将整数转换为以“ 0x”为前缀的小写十六进制字符串。 If x is not a Python int object, it has to define an index () method that returns an integer.
如果x不是Python int对象,则它必须定义一个返回整数的index ()方法。
try binascii.unhexlify
: 尝试
binascii.unhexlify
:
Return the binary data represented by the hexadecimal string hexstr.
返回由十六进制字符串hexstr表示的二进制数据。
example: 例:
assert binascii.unhexlify('3f4800003f480000') == b"\x3f\x48\x00\x00\x3f\x48\x00\x00"
>>> hex(int('3f4800003f480000', 16))
'0x3f4800003f480000'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.