This question is related to this post :
int to binary python
I have this sequence number I need to increment, now it’s fixed and working fine.
But, I just found out that actually the sequence number is not simply a short, consider this scheme:
12 bits 4 bits
Sequence number Fragment number
So if I send a sequence number with the value 258 (\x02\x01), it will actually be;
Fragment number :2
Sequence number :16
I’m wondering how i can have that 258 to be the actual sequence number, and not splitted this way.
Is there any simple way to achieve this with struct ?
UPDATE:
What i meant in my above question was:
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"),
###12 bits of this 'short' is Seq num, 4 bits left are if this is a fragment or not(if it is, its value will be =>1, and will then represent the frag number.)
])
Later, I call my class and send the packet like this:
send(PacketHeader(Sequencenum=257))
send(PacketHeader(Sequencenum=258))
send(PacketHeader(Sequencenum=259))
I need to convert to a short the sequence number (in this case lets say 257), and take care of the 4 bits that represent the fragment number in that 16 bit short. I would prefer to always have fragment number set to 0, but have Sequencenum incremented correctly.
Thanks in advance !
Something along the line:
And back: