I'm trying to find two bytes inside binary file, then increase value of those two bytes and replace them inside file. Those two bytes are on positions 0x82-0x83. For now on I have successfully extracted those two bytes using this:
#!/usr/bin/env bash
BYTES=$(tail -c +131 "$1" | head -c 2)
Those bytes have value: 1B 1F
. I'm stuck with:
- How to convert bytes to integer? It should be
6943
decimal. - How to append / echo binary data to file
- How to write increased bytes inside file on positions 0x82-0x83. I could use
head -c 130 original.bin >> new_file.bin && magic_command_writing_bytes_to_file >> new_file.bin && tail -c +133 original.bin
, but there must be better way.
I could do that in PHP, it should be easier, but I'm interested how to do this in bash.
Best Answer
Testing with this file:
So in this case the
1B 1F
is at position12
.Convert to integer (there is probably an easier way)
And the reverse:
And putting it back in the file:
Result: