简体   繁体   中英

Replace hex in binary file with shell without sed

I have binary file that I want to replace with shell script 0xaa,0xbb,0xcc,0xdd with 0x11,0x22,0x33,0x44

I can get 0xaa,0xbb,0xcc,0xdd into shell script like $1 but how can I replace it with 0x11,0x22,0x33,0x44 without sed?

You could create a bash script that does a hexdump of your file, a simple replace and then convert back to a binary file.

You could use an application such as xxd, or hexdump to create a hex dump of your file. This is explained here: convert binary data to hex in shell script?

It looks like xxd creates a hex string that looks like this "aabbccdd" rather than your "0xaa,0xbb,0xcc,0xdd" so watch out for that!

To find out how to do a replace, you could look at this answer: Find and Replace Inside a Text File from a Bash Command

You would then want to convert the HEX string back into binary, which is explained here: linux shell scripting: hex string to bytes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM