3

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.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? 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. 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))

1
  • Sorry, tryna' keep it "real"... - hip 1 min ago Commented Oct 29, 2010 at 14:46

2 Answers 2

5

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Nope, that does not result in hex values, that results in decimal values.
That's just the representation. Integers are numbers. Hexadecimal and decimal and octal are ways of representing the number. 0x34 and 52 are the same number. If you want to print the number in hexadecimal, that's a different matter altogether, and that's where the 'x' string format comes along.
Thank you for your answers and coments. The matter has been cleared now. Sorry for not accepting your answer @Mark, I liked the extra map thrown in by @MAK, otherwise you were the first and to the point. Thanks.
There's also the hex() built-in for converting a number to a hexadecimal string representation, including leading 0x.
4

Why don't you just build a list of ints and just print them out in base 16 (either by using "%x"%value or hex) instead? If the values are given to you in this form (e.g. from some other source), you can use int with the optional second parameter to turn this into an 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

5 Comments

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. Thank you for your answer.
@Soulseekah: All values are binary under the hood. Hex or decimal is only the representation when you convert it to a string when printing. So, distinctions like "hex number" and "decimal number" are meaningless for values stored in variables.
I need packetPcap[:len(data)] = packHex where packHex contains something like (0x00, 0x1F, 0x3C, 0x84, 0x70, 0x22, 0x1C, 0xAF, 0xF7). Thank you for your help. +1
map(hex,packet) is interesting. So basically it doesn't matter whether the list/tuple contains hex or dec, it's all just bits in the end. I can supply (0x00, 39, 29, 0x32) and everything is going to be parsed into decimals. I can then use map(hex, to visually get hex for better reading. Thank you for your answers.
@Soulseekah: Yes. Except, everything gets parsed into binary not decimals. The default base that Python expects in literals is decimal, but even that is converted to binary once it is read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.