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)?
Asked
Active
Viewed 166 times
1 Answers
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 filedumps the binary datafilein continuous hexdump style.fold -w2wraps the input lines by every two characters (= every bytes).perl -pe 's/00/ff/ || s/ff/00/'swaps00andffin the input string. The||logic works asif .. else ..condition. Otherwise the input00is once converted toffand immediately converted back to00again.xxd -r -pis the reversed version ofxxd -pwhich 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
-
1Thank you for your help and patience :) – janjin Mar 01 '22 at 14:35