我在下面提供一个函数来实现这个功能:
hex2bin path/to/myfirmware1.hex
hex2bin path/to/myfirmware1.hex path/to/myfirmware2.hex
hex2bin myfirmware1.hex myfirmware2.hex myfirmware3.hex myfirmware4.hex
注意:在我的
eRCaGuy_dotfiles存储库中获取我
hex2bin
和
hex2xxdhex
函数的最新版本,请点击这里:
.bash_useful_functions。
Bash函数用于批量转换Intel *.hex
固件文件为*.bin
固件文件,并生成用于在meld
中进行比较的*.xxd.hex
文件
补充@A. Genchev's answer:当我想一次转换多个十六进制文件时,我发现使用如此长的命令很繁琐,所以我编写了这个辅助函数。
将此内容复制并粘贴到您的
~/.bashrc
文件底部,然后运行
. ~/.bashrc
以
重新源您的
~/.basrhc
文件,并使此功能对您可用:
hex2bin() {
for filepath_hex in "$@"; do
filepath_hex_no_extension="${filepath_hex%.*}"
filepath_bin="${filepath_hex_no_extension}.bin"
echo "Converting \"$filepath_hex\" to \"$filepath_bin\"."
objcopy --input-target=ihex --output-target=binary \
"$filepath_hex" "$filepath_bin"
done
}
alias gs_hex2bin="hex2bin"
如果您使用不同的工具链,只需将
objcopy
替换为您工具链中相应版本的
objcopy
。例如,对于适用于 PIC 微控制器的
Microchip MPLAB XC32 编译器工具链,请使用
xc32-objcopy
而非
objcopy
。
现在,不再需要这样写:
objcopy --input-target=ihex --output-target=binary \
path/to/myfirmware1.hex path/to/myfirmware1.bin
objcopy --input-target=ihex --output-target=binary \
path/to/myfirmware2.hex path/to/myfirmware2.bin
...你可以这样做:
hex2bin path/to/myfirmware1.hex path/to/myfirmware2.hex
您可以一次性传入尽可能多的路径名。
深入了解:使用 objcopy
、xxd
和 meld
进行比较和分析十六进制/二进制文件差异
如果您想要比较两个英特尔十六进制固件文件以查找差异,该怎么办?也许两个十六进制固件镜像几乎相同,但仅在其中存储的某些字符串、IP 地址或时间戳方面存在差异。那将很好知道。
具体步骤如下:
hex2xxdhex "path/to/myfirmware1.hex" "path/to/myfirmware2.hex"
meld "path/to/myfirmware1.xxd.hex" "path/to/myfirmware2.xxd.hex"
在
meld
中的示例输出:
![enter image description here](https://istack.dev59.com/EoOau.webp)
![enter image description here](https://istack.dev59.com/Mzf5E.webp)
这是我
hex2xxdhex
函数的定义:
hex2xxdhex() {
for filepath_hex in "$@"; do
filepath_hex_no_extension="${filepath_hex%.*}"
filepath_bin="${filepath_hex_no_extension}.bin"
filepath_xxdhex="${filepath_hex_no_extension}.xxd.hex"
echo "Converting \"$filepath_hex\" to \"$filepath_bin\" and to"\
"\"$filepath_xxdhex\"."
objcopy --input-target=ihex --output-target=binary \
"$filepath_hex" "$filepath_bin"
xxd "$filepath_bin" "$filepath_xxdhex"
done
}
alias gs_hex2xxdhex="hex2xxdhex"
要获取更多详细信息,请查看我的答案:超级用户:如何使用meld
比较二进制文件、十六进制文件和Intel Hex固件文件