byte reverse AB CD to CD AB with python

十年热恋 提交于 2019-11-30 05:05:45

问题


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.


回答1:


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 :-)




回答2:


In Python 3.4 you can use this:

>>> data = b'\xAD\xDE\xDE\xC0'
>>> swap_data = bytearray(data)
>>> swap_data.reverse()

the result is

bytearray(b'\xc0\xde\xde\xad')



回答3:


data = b'\xAD\xDE\xDE\xC0'
reversed_data = data[::-1]
print(reversed_data)
# b'\xc0\xde\xde\xad'



回答4:


Python has a list operator to reverse the values of a list --> nameOfList[::-1]

So, I might store the hex values as string and put them into a list then try something like:

def reverseList(aList):
rev = aList[::-1]
outString = ""
for el in rev:
    outString += el + " "
return outString


来源:https://stackoverflow.com/questions/14543065/byte-reverse-ab-cd-to-cd-ab-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!