0

This question is probably very easy for most of you, but i cant find the answer so far.

I'm building a network packet generator that goes like this:

class PacketHeader(Packet):
fields = OrderedDict([
    ("head", "\x00"),
    ("headpad", "\x00"),
    ("headlen", "\x1a\x00" ),
    ("presentflag", "\x2e"),
    ("timestamp", "\x43\x10\x58\x16\xa1\x01\x00\x00"),
    ("flag", "\x03"),
    ("Sequencenum", "\x04\x00"),

])

Later, I call my class and send the packet like this:

send(PacketHeader(Sequencenum=257))
send(PacketHeader(Sequencenum=258))
send(PacketHeader(Sequencenum=259))

Unfortunatly, on the wire i have \x32\x35\x37 (257 in hex) instead of having this :\x01\x01.

What i would like to do, is being able to:

send(PacketHeader(Sequencenum=257+1)) 

and have it converted to int correctly on the wire

Thanks in advance !

2
  • 2
    I'm confused by perhaps too much information :-) Are you trying to convert an integer to hex? Can you give a simple input and desired output example? Commented Jul 10, 2012 at 20:54
  • @martineau: My mistake, you should have read : "\x01\x01". Will modify my post. Commented Jul 10, 2012 at 22:00

3 Answers 3

3

If you want to convert int or any other primitive data type to binary then the struct module might help you.

http://docs.python.org/library/struct

>>> import struct
>>> struct.pack('>i', 257)
'\x00\x00\x01\x01'
>>> struct.pack('<i', 257)
'\x01\x01\x00\x00'
>>> struct.unpack('<i', '\x01\x01\x00\x00')
(257,)
>>> struct.unpack('>i', '\x00\x00\x01\x01')
(257,)

ms4py mentioned you might want to convert 2 byte ints so:

>>> struct.pack('>h', 257)
'\x01\x01'
>>> struct.unpack('>h', '\x01\x01')
(257,)
>>> 

oh and:

>>> struct.unpack('>h', '\x10\x00')
(4096,)
>>> 
Sign up to request clarification or add additional context in comments.

3 Comments

Probably he wants a short (two bytes for an int): >>> struct.pack('!h', 258) '\x01\x02'
maybe your right, I have no idea :( ... either way the link to the struct should help him decide how he wants to do the conversion, I'll add the example anyway, thank you.
I should have thought about this !! Thanks for your answer.
0

If you have an integer and want hex:

hex(257)
'0x101'

Other way around

int('0x101', 16)
257

4 Comments

That's what I thought too .. but seems almost too straight forward :) Maybe OP will clarify
@Levon Agreed - but figured I'd take a punt ;)
Who knows, this may just be the ticket .. you'll get my upvote too if OP likes it.
@Levon I do like the struct answer though - more portable
-1

Seems to me, that you want an integer to binary converter.

If that's the case, then this should work:

def convert(N):
    if not N:
        return ''
    else:
        return "%s%s" %(convert(N/2), N%2)

Hope this helps

4 Comments

bin(N)[2:] will already do that :) .. " batteries included" and all that. (didn't downvote!)
did NOT know about bin(n). Oh God! the number of times I've done this! I've wasted so many LOC in so many modules over the years sheds a silent tear
@Levon the code is the same as '{:b}'.format(257) I think (if you don't want the leading 0x stuff...)
Downvoter: Would you like to give me a reason so I can upgrade my answer?

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.