Вопрос

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.

Это было полезно?

Решение 2

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

Другие советы

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')
data = b'\xAD\xDE\xDE\xC0'
reversed_data = data[::-1]
print(reversed_data)
# b'\xc0\xde\xde\xad'

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

Python3

bytes(reversed(b'\xAD\xDE\xDE\xC0'))
# b'\xc0\xde\xde\xad'
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top