modify a byte in a binary file using standard linux command line tools
# read 1 byte at offset 40Cb_hex=$(xxd -seek $((16#40C)) -l 1 -ps A.bin -)# delete 3 least significant bitsb_dec=$(($((16#$b_hex)) & $((2#11111000))))cp A.bin B.bin# write 1 byte back at offset 40Cprintf "00040c: %02x" $b_dec | xxd -r - B.bin
Tested in bash
and zsh
on OSX and Linux.
The last line explained:
00040c:
is the offsetxxd
should write to%02x
converts$b
from dec to hexxxd -r - B.bin
: reverse hex dump (xxd -r
), take line number and hex stdin (-
) and write toB.bin