pythonbytereversing

byte reverse AB CD to CD AB with python


I have a .bin file, and I want to simply byte reverse the hex data. Say for instance @ 0x10 it reads AD DE DE C0, want it to read DE AD C0 DE.

I know there is a simple way to do this, but I am am beginner and just learning python and am trying to make a few simple programs to help me through my daily tasks. I would like to convert the whole file this way, not just 0x10.

I will be converting at start offset 0x000000 and blocksize/length is 1000000.

here is my code, maybe you can tell me what to do. i am sure i am just not getting it, and i am new to programming and python. if you could help me i would very much appreciate it.

def main():
    infile = open("file.bin", "rb")
    new_pos = int("0x000000", 16)
    chunk = int("1000000", 16)
    data = infile.read(chunk)
    reverse(data)

def reverse(data):
    output(data)

def output(data):
    with open("reversed", "wb") as outfile:
        outfile.write(data)

main()

and you can see the module for reversing, i have tried many different suggestions and it will either pass the file through untouched, or it will throw errors. i know module reverse is empty now, but i have tried all kinds of things. i just need module reverse to convert AB CD to CD AB. thanks for any input

EDIT: the file is 16 MB and i want to reverse the byte order of the whole file.


Solution

  • In Python 2, the binary file gets read as a string, so string slicing should easily handle the swapping of adjacent bytes:

    >>> original = '\xAD\xDE\xDE\xC0'
    >>> ''.join([c for t in zip(original[1::2], original[::2]) for c in t])
    '\xde\xad\xc0\xde'
    

    In Python 3, the binary file gets read as bytes. Only a small modification is need to build another array of bytes:

    >>> original = b'\xAD\xDE\xDE\xC0'
    >>> bytes([c for t in zip(original[1::2], original[::2]) for c in t])
    b'\xde\xad\xc0\xde'
    

    You could also use the < and > endianess format codes in the struct module to achieve the same result:

    >>> struct.pack('<2h', *struct.unpack('>2h', original))
    '\xde\xad\xc0\xde'
    

    Happy byte swapping :-)