情景:RH850单片机通过bootloader刷写程序需要FlashDriver文件,而我们的任务是将这个文件中的地址部分提取出来,变成0xFF的格式。
一种参考方法,领导发我的,我嫌麻烦没用,有兴趣的同学可以看一下
https://www.cnblogs.com/xiaochuanshuo/p/15069088.html
---------------------------------------------------------------------------------------------------------------------------------
1、将目标hex文件转为txt文件,方便操作,直接改后缀名就可以了。记得将文件名与delete1的目标文件名对应一下,已在下文标出。
2、打开文件之后我们发现,第一行和最后一行不是我们的目标,每行的前9位和后2位也不是我们的目标,将他们删除。删除第一行和最后一行,与删除每行前9位与后2位的方法不太相同。
(1)删除第一行和最后一行:命名为delete1.bat
@echo off
setlocal enabledelayedexpansion
set "inputFile=input.txt" //这里,目标文件名
set "outputFile=output.txt"
set "tempFile=temp_file.txt"
rem 删除第一行
more +1 %inputFile% > %tempFile%
del %inputFile%
ren %tempFile% %inputFile%
rem 删除最后一行
set /a lineCount=0
for /f %%a in ('find /c /v "" ^< %inputFile%') do set /a lineCount=%%a
set /a count=0
(for /f "tokens=*" %%a in (%inputFile%) do (
set /a count+=1
if !count! lss %lineCount% echo %%a
)) > %outputFile%
echo Done.
(2)删除前9位与后2位:命名为delete2.bat
batch
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (output.txt) do (
set line=%%a
echo !line:~9,-2!>>output1.txt
)
endlocal
3、添加0x,同时在每组数据后加“,”分隔:命名为Add.bat
@echo off
setlocal enabledelayedexpansion
set "inputFile=output1.txt"
set "outputFile=zyour_output_file.txt"
for /f "tokens=*" %%a in (%inputFile%) do (
set "line=%%a"
set "outputLine="
for /l %%i in (0, 2, 999) do (
set "char=!line:~%%i,2!"
if "!char!" neq "" (
set "outputLine=!outputLine!0x!char!,"
)
)
echo !outputLine:~0,-1!, >> %outputFile%
)
echo Done.
4、到这里就可以结束了,但是毕竟是给上边的,中间文件要删除,原始文件要保留
(1)删除中间文件:命名为delete3.bat
@echo off
REM 定义要删除的文件名
set "file1=input.txt"
set "file2=output.txt"
set "file3=output1.txt"
REM 删除文件
del "%file1%" "%file2%" "%file3%"
echo Done.
(2)保留原始文件,命名为copy.bat
@echo off
set "sourceFile=input.txt"
set "destinationFile=zinput_copy.txt"
REM 定义源文件和目标文件名
REM 复制并重命名文件
copy /Y "%sourceFile%" "%destinationFile%"
echo Done.
5、最后,把所有脚本串联起来就可以了
@echo off
call copy.bat
ping -n 2 127.0.0.1 > nul
call delete1.bat
ping -n 2 127.0.0.1 > nul
call delete2.bat
ping -n 2 127.0.0.1 > nul
call Add.bat
ping -n 2 127.0.0.1 > nul
call delete3.bat
echo Done.