Reverse String but keep character pairs together

SNI*_*I0N 1 python string reverse python-3.x

I have analysed the global header of a PCAP file and found out that the magic number is: d4 c3 b2 a1

This means that it uses a little endian and all the bytes that come after it need to processed in reverse order. The other sections of the global header are as follows:

major version =  02 00

minor version =  04 00

time zone =  00 00 00 00

timestamp =  00 00 00 00

snaplen =  ff ff 00 00

linktype =  01 00 00 00
Run Code Online (Sandbox Code Playgroud)

However given that little endian is in use, it should be:

major version =  00 02

minor version =  00 04

time zone =  00 00 00 00

timestamp =  00 00 00 00

snaplen =  00 00 ff ff

linktype =  00 00 00 01
Run Code Online (Sandbox Code Playgroud)

So I need a way to reverse the string but still keep the pairs of characters separated by the spaces in the same order.

So the code [::-1] will not work because it transforms 02 00 into 00 20, whereas it needs to be 00 02

How would I go about doing this?

adr*_*tam 6

You need a few more steps to do it:

linktype = "00 00 00 01"
rev_linktype = " ".join(reversed(linktype.split()))
Run Code Online (Sandbox Code Playgroud)

The idea is to first chop the string into tokens instead of characters, then reverse the order of the tokens, and then join them back into one long string.