Write byte at address (hexedit/modify binary from the command line) Write byte at address (hexedit/modify binary from the command line) shell shell

Write byte at address (hexedit/modify binary from the command line)


printf '\x31\xc0\xc3' | dd of=test_blob bs=1 seek=100 count=3 conv=notrunc 

dd arguments:

  • of | file to patch
  • bs | 1 byte at a time please
  • seek | go to position 100 (decimal)
  • conv=notrunc | don't truncate the output after the edit (which dd does by default)

One Josh looking out for another ;)


The printf+dd based solutions do not seem to work for writing out zeros. Here is a generic solution in python3 (included in all modern distros) which should work for all byte values...

#!/usr/bin/env python3#file: set-byteimport sysfileName = sys.argv[1]offset = int(sys.argv[2], 0)byte = int(sys.argv[3], 0)with open(fileName, "r+b") as fh:    fh.seek(offset)    fh.write(bytes([byte]))

Usage...

set-byte eeprom_bad.bin 0x7D00 0set-byte eeprom_bad.bin 1000 0xff

Note: This code can handle input numbers both in hex (prefixed by 0x) and dec (no prefix).


Here's a Bash function replaceByte, which takes the following parameters:

  • the name of the file,
  • an offset of the byte in the file to rewrite, and
  • the new value of the byte (a number).
#!/bin/bash# param 1: file# param 2: offset# param 3: valuefunction replaceByte() {    printf "$(printf '\\x%02X' $3)" | dd of="$1" bs=1 seek=$2 count=1 conv=notrunc &> /dev/null}# Usage:# replaceByte 'thefile' $offset 95