0

So what i am trying to achieve is the auto-editing of hex files. For example - The user enters a hex address, and the hex value to replace at that address. This change is to be applied to 10 different binary files

Can anyone point me in the direction of auto-editing files(emacs auto opens the file, switches to hexl mode, jumps to address provided by user, inserts hex code at that address which is also provided by the user and saves file then finally exits) So far i have only been able to achieve auto opening to hexl mode by launching it like

emacs file -f hexl-mode -f hexl-goto-hex-address

From here on i have no idea how to input the hex address and then move onto changing the code

Drew
  • 75,699
  • 9
  • 109
  • 225
TheArmKing
  • 13
  • 2
  • 2
    Does it have to be inside Emacs? This seems like it would be simpler to do in some sort of script run from your terminal. – zck Aug 08 '18 at 17:54
  • Yes actually, i am using AppleScript to send keystrokes. But it would be better if there was a way to do it through Emacs. I am trying to develop this Tool for Mac as well as Windows, but i dont know an equivalent for AppleScript on Windows – TheArmKing Aug 08 '18 at 18:07
  • You can do "auto editing" by writing an Emacs function in elisp. Start `C-h i` and select "Emacs Lisp Intro" for an introduction and "elisp" for a reference manual. The function need to visit a the, perform the edit, and save the file. By the way, you don't have to use hexl, you can simply open the file and write an elisp function that replaces the characters you want replaced. – Lindydancer Aug 08 '18 at 19:13
  • 1
    @TheArmKing I was thinking of writing, for example, a Python script to do this. I don't know if Emacs is the right tool. Why did you want to use Emacs specifically? – zck Aug 08 '18 at 19:34
  • @Lindydancer i will check it out, Thanks! – TheArmKing Aug 09 '18 at 08:09
  • @zck Tbh i was lookong for an auomation script, i didn't know this could be achieved using python, so i looked out for "terminal hex editors" and found emacs to be the best one, if you do have a py script that can be used to edit binary that would be nice as well – TheArmKing Aug 09 '18 at 08:11
  • Python definitely can do this. I'm unfamiliar with doing so, but it seems that you might want the mmap library, as suggested by this question: https://stackoverflow.com/questions/1322508/python-how-to-edit-hexadecimal-file-byte-by-byte – zck Aug 09 '18 at 19:25
  • Solved by using HexCurse and Applescript to Automate file-editing process – TheArmKing Aug 15 '18 at 16:41

1 Answers1

1

The following call to emacs inserts the hexadecimal number \x0b at character position 10 of the files 1st.bin and 2nd.bin.

emacs --batch --eval '(dolist (file-name (list "1st.bin" "2nd.bin")) (find-file-literally file-name) (goto-char 10) (insert "\x0b") (save-buffer 0) (kill-buffer))'
Tobias
  • 32,569
  • 1
  • 34
  • 75
  • Answers the question well but not convinent for me, thus my proposal of using applescript and hexcurse instead. – TheArmKing Aug 16 '18 at 12:19