1

There already is a beautiful trick in this thread to write bytes to binary file at desired address with dd ,is there any way to swap bytes(e.g swap 0x00 and 0xFF), or replace bytes with common tools (such as dd)?

janjin
  • 13
  • 2

1 Answers1

0

Would you please try the following:

xxd -p input_file | fold -w2 | perl -pe 's/00/ff/ || s/ff/00/' | xxd -r -p > output_file
  • xxd -p file dumps the binary data file in continuous hexdump style.
  • fold -w2 wraps the input lines by every two characters (= every bytes).
  • perl -pe 's/00/ff/ || s/ff/00/' swaps 00 and ff in the input string. The || logic works as if .. else .. condition. Otherwise the input 00 is once converted to ff and immediately converted back to 00 again.
  • xxd -r -p is the reversed version of xxd -p which converts the input hexadecimal strings into binaries.
tshiono
  • 17,571
  • 2
  • 13
  • 19
  • Thanks for your suggestions, and can we do this with commmon tools: Let the start of a file is _60 15 00 FF C2 15 00 26_ , After some oprations, it becomes _60 15 FF 00 C2 15 FF 26_ , _0xFF_ replaced by _0x00_, and vice versa -- that means I may not know what position _0x00_ or _0xFF_ is, just replace/swap some part of a file e.g 1 kB at the start. – janjin Feb 21 '22 at 10:28
  • Oh, I had misunderstood your requirement. I've fixed my answer. This is a quick hack and may be improved in execution time but will work. – tshiono Feb 21 '22 at 12:10
  • 1
    Thank you for your help and patience :) – janjin Mar 01 '22 at 14:35