0

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.

EDIT:

here is my code, maybe you can tell me where i am messing up.

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

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

main()

how would i go about coding it to byte reverse from CDAB TO ABCD? if it helps any the file is exactly 16MB

james28909
  • 554
  • 2
  • 9
  • 20

1 Answers1

0

You can just swap the bytes manually like this:

with open("file.bin", "rb") as infile, open("reversed", "wb") as outfile:
  data = infile.read()
  for i in xrange(len(data) / 2):
    outfile.write(data[i*2+1])
    outfile.write(data[i*2])
Diego Basch
  • 12,764
  • 2
  • 29
  • 24
  • Can you paste the full error you get? Are you indenting the code correctly? It sounds like the two `write` lines may not be indented under the if. – Diego Basch Jan 27 '13 at 00:31
  • how about this, lemme upload to you my .py and the file.bin – james28909 Jan 27 '13 at 01:15
  • what would be optimum is if i could just run the .py file and it reverse it to AB CD, then run it again for CD AB. and i really appreciate your help this far :)[link](https://dl.dropbox.com/u/64707444/New%20folder%20%282%29.rar) – james28909 Jan 27 '13 at 01:24
  • but it does not reverse the hex data – james28909 Jan 27 '13 at 02:04
  • if you have a hex editor... open the file before and after with a hex editor, you will see what i mean. should convert hex from CD AB to AB CD. pretty much all this is doing is copying and saving the original "file.bin" as "reversed" which is awesome.... but i want it to convert the hex from CD AB to AB CD before it saves the "reversed" file. – james28909 Jan 27 '13 at 02:08
  • the original file has "0F AC E0 FF 00 00 00 00 DE AD BE EF" i want it to reverse it like this "AC 0F FF E0 00 00 00 00 AD DE EF BE" – james28909 Jan 27 '13 at 02:29
  • Is your file binary, or do you have text representing hex in it? My snippet reverses the order of each pair of bytes in a binary file. See: http://pastebin.com/0U6bQGcd – Diego Basch Jan 27 '13 at 02:59
  • [link](https://dl.dropbox.com/u/64707444/New%20folder%20%282%29.rar) open the file.bin in a hex editor. i believe its hex binary – james28909 Jan 27 '13 at 03:31
  • also, that pastebin is exactly what i need i believe, could you possibly give me a little more insight on how you accomplished that? please remember, i am only 3-4 months into python, and no other expirence coding. – james28909 Jan 27 '13 at 03:33
  • Run this program exactly as it is. I just ran it on your file and it works. https://gist.github.com/4646154 – Diego Basch Jan 27 '13 at 03:44
  • Traceback (most recent call last): File "C:/Users/James/Desktop/New folder (7)/jjj.py", line 3, in for i in xrange(len(data) / 2): NameError: name 'xrange' is not defined – james28909 Jan 27 '13 at 03:46
  • that was what the problem was man. i had 3.3.0 i downgraded to 2.7.3? and it worked fabulous. that you very much for all your help man, your awesome >:D – james28909 Jan 27 '13 at 03:58