简体   繁体   English

在Python中将0x字符串解释为十六进制

[英]Interpret 0x string as hex in Python

I'm appending some hex bytes into a packet = [] and I want to return these hex bytes in the form of 0x__ as hex data. 我将一些十六进制字节附加到packet = [] ,我希望以0x__的形式返回这些十六进制字节作为十六进制数据。

packet.append("2a")
packet.append("19")
packet.append("00")
packet.append("00")

packHex = []

for i in packet:
    packHex.append("0x"+i) #this is wrong

return packHex

How do I go about converting ('2a', '19', '00', '00') in packet to get (0x2a, 0x19, 0x0, 0x0) in packHex ? 如何在packet转换('2a', '19', '00', '00')以获取packHex (0x2a, 0x19, 0x0, 0x0) packHex I need real hex data, not strings that look like hex data. 我需要真正的十六进制数据,而不是十六进制数据的字符串。

I'm assembling a packet to be sent over pcap , pcap_sendpacket(fp,packet,len(data)) where packet should be a hexadecimal list or tuple for me, maybe it can be done in decimal, haven't tried, I prefer hex. 我正在组装一个要通过pcap发送的数据包, pcap_sendpacket(fp,packet,len(data)) ,其中packet应该是十六进制列表或者元组,也许它可以用十进制表示,没试过,我更喜欢十六进制。 Thank you for your answer. 谢谢您的回答。

packetPcap[:len(data)] = packHex

Solved: 解决了:

for i in packet: packHex.append(int(i,16))

If output in hex needed, this command can be used: print ",".join(map(hex, packHex)) 如果需要以十六进制输出,可以使用此命令: print ",".join(map(hex, packHex))

You don't really want 'hex data', you want integers. 你真的不想要'十六进制数据',你想要整数。 Hexadecimal notation only makes sense when you have numbers represented as strings. 只有将数字表示为字符串时,十六进制表示法才有意义。

To solve your problem use a list comprehension: 要解决您的问题,请使用列表解析:

[int(x, 16) for x in packet]

Or to get the result as a tuple: 或者将结果作为元组获得:

tuple(int(x, 16) for x in packet)

Why don't you just build a list of int s and just print them out in base 16 (either by using "%x"%value or hex ) instead? 为什么不直接构建一个int list并将它们打印在基数16(通过使用"%x"%valuehex )代替? If the values are given to you in this form (eg from some other source), you can use int with the optional second parameter to turn this into an int . 如果值(从其他来源例如)给你以这种形式,可以使用int与可选的第二个参数把它变成一个int

>>> int('0x'+'2a',16)
42
>>> packet=["2a","19","00","00"]
>>> packet=[int(p,16) for p in packet]
>>> packet
[42, 25, 0, 0]
>>> print ", ".join(map(hex,packet))
0x2a, 0x19, 0x0, 0x0

Say you have a list = ["2a", "19", "00", "00"] 假设你有一个list = ["2a", "19", "00", "00"]

For me I use list = [hex(int(i, 16)) for i in vals] 对我来说list = [hex(int(i, 16)) for i in vals]使用list = [hex(int(i, 16)) for i in vals]

It then returns a new list with hex type of all the elements. 然后它返回一个包含所有元素的十六进制类型的新列表。

>>>['0x2a', '0x19', '0x0', '0x0']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在字符串中的十六进制数字前面添加“0x” - Appending '0x' before the hex numbers in a string 如何在 Python 中使用没有 0x 的 hex()? - How to use hex() without 0x in Python? 将十六进制转换为字符串文字:用\\ x替换0x - Convert hex into string literal: replacing 0x with \x Python 十六进制字符串中 0x 和 \x 的含义有什么区别? - What is the difference between the meaning of 0x and \x in Python hex strings? 将 int 格式化为带有 0x 前缀的大写十六进制字符串? - Format int as an uppercase hex string with 0x prefix? 如何在 Python 中获取十六进制代码行 (\\x) 到十六进制地址 (0x)? - How to get line of hex codes (\x) to hex address (0x) in Python? 在 Python 中使用“0x”和前导 0 将字符串转换为十六进制 - Convert string to hexadecimal with “0x” and leading 0s in Python 使用 Python 将 INT com 0x 前缀转换为字符串 - Convert INT com 0x prefix to String with Python 如何在小尾数中打印十六进制数字,填充零,并且在python中不显示'0x'? - How to print hex number in little endian, padded zeroes, and no '0x' in python? 将MiB / KiB转换为十六进制'0x'表示法并添加(Python)的脚本 - Script to convert MiB/KiB to Hex '0x' notation and do additions (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM