11 March 2016 - Forum Rules

Main Menu

How to reverse the order of the numbers of a file?

Started by LuizMelos, June 04, 2018, 03:27:02 AM

Previous topic - Next topic


Good Morning. I have a binary file and the numbers are inverted. How can I reverse their order?
34 27 45 25 to 27 34 25 45.


Uh, why do you want to do that? Is it a pointer table?

Regardless, I'd do it my own special way, but others might think it's weird. :) I'd copy the bytes I wanted into Textpad from HxC, replace spaces with several tabs, reduce the window so there's only the first two visible, reformat (Ctrl-Shift-J) so there's now two bytes per line, replace tabs with nothing, block select the second bytes on each line, cut, paste in front of the others.

Again, that's what I do, maybe it's no use to you. :)


The whole file or just a few bytes at a time?

If it is just a few bytes at a time then it is not inverted (by the way inverted will tend to mean 0 becomes 1 and vice versa, however you made your meaning clear for this) but actually the rest of endianness. If you feel the need to sort it rather than just handle it in your head then a hex editor with a byte flip command will be able to help you here. How many bytes likely to be involved with this varies between systems and whatever the devs were feeling that day but tends to match how many bits are in the main registers.
For more exotic stuff (though still describable in a single sentence or so) that bog standard hex editors will not have then I do often use methods like Psyklax describes with a text editor, spreadsheet, column select (available for both text editors and spreadsheets these days) and find/replace commands.

If it is the whole file then "why?" becomes the question. Only thing I can imagine is some weird arrangement with a dumping tool manually pulling things from a chip. I don't know how I would do this simply. It would probably take me actually coding something, most languages will be able to do it for a simple selection, array or stream ( ...).
If it can't do the whole thing at once then iterate it over the whole file; not elegant and likely quite resource intensive but will get you there in a human acceptable timeframe which is all most around here care about.

Gideon Zhi

Assuming you've read your file into a variable called byte_array, here's a python snippet:

new_list = []
for i in range(0, len(byte_array), 2):
    new_list.extend([byte_array[i+1], byte_array[i]])


Mid-Big Endian (BADC) is what I need.
is there a program that converts my file to this format?