Board logo

标题: [文本处理] 批处理版HexFile2Ascii [打印本页]

作者: hfg1977    时间: 2012-2-3 04:54     标题: 批处理版HexFile2Ascii

不解释了,Debug用法google吧,很多的.
  1. @goto start
  2. :help
  3. @echo.======== Ver:1.0 ==================== 发布日期: 2012-02-03 ==============
  4. @echo. BAT_NAME: HexFile2Ascii.bat
  5. @echo. 返回值: %def_filename%
  6. @echo. 返回值获得: ┌ set /p strHEX=^<%def_filename%
  7. @echo. └ echo %%strHEX%%
  8. @echo. 调用: call HexFile2Ascii.bat HexFileName [outFileName]
  9. @echo. 说明:
  10. @echo. 本批处理是从Ascii2Hex.bat中分离出来的,默认系统XP.
  11. @echo. 转载必须指明出处: "转载自批处理之家 bbs.bathome.net"
  12. @echo. HexFile必须是 "0E 02 56 FE" 形式,以空格分隔
  13. @echo. 使用第三方工具sed对原始文件预处理,如果没有则跳过该过程.
  14. @echo. 大小限制: ASCII文件 65279 Byte HEX文件65279*3=195837 Byte
  15. @echo.
  16. @echo.====================== Copyright@ by hf-g [彭城] ========================
  17. ping /n 4 127.0>nul&
  18. goto:end
  19. ::==============================================================
  20. :start
  21. @echo off
  22. setlocal enabledelayedexpansion
  23. ::==============================================================
  24. set "def_filename=ret_str.txt"
  25. if /i "%~1"=="" goto help
  26. if /i "%~1"=="-?" goto help
  27. for /f "tokens=2" %%a in ('CHCP') do (if not "%%a"=="437" (mode con cols=80 lines=25&color 07&CHCP 437&graftabl 936))>nul
  28. ::工作路径设置:
  29. (pushd %~dp1&&pushd %~dp2&&pushd %~dp3&&pushd %~dp4)>nul
  30. echo CD=%cd%
  31. set "char="&set "filename=nul"&set "dec_size="&set "hex_str="&set "ascChar="&set "des_filename=nul"
  32. set /a char_BL=0,filename_BL=0,asc2hex_BL=0,hex2asc_BL=0,IPadd_BL=0,exit_BL=0
  33. set /a des_filename_BL=0
  34. set /a line=0,line_YU=0,count=0,num=0
  35. ::================================ Mian ==============================
  36. path=%path%;%~d0\DOS\网络;%~d0\DOS\command\第三方命令行程序;%~d0\DOS\command;%~d0\DOS\源码;%~d0\DOS\sysbkup
  37. if not "%~2"=="" set "def_filename=%~2"
  38. if not exist "%~1\." (if not exist "%~1" (echo ERR:Hex文件不存在&goto:end))
  39. ::用第三方工具sed对原始文件预处理,如果没有则跳过该过程. 文件给予备份.
  40. sed --v 1>nul 2>nul&&(
  41. if not exist "%~nx1.bak" copy "%~nx1" "%~nx1.bak"
  42. sed -i "s/\s*$//;s/^\s]*//;/^$/d" "%~1"
  43. )||(
  44. echo 第三方工具 sed 没找到.&echo........
  45. )
  46. call:file2asc "%~1" "%def_filename%"
  47. :end
  48. pause&pause
  49. ::echo ☆☆☆
  50. del tempfile.txt strHEX.txt tmpHEX.txt>nul 2>nul
  51. del sed??????
  52. popd&popd&popd&popd
  53. exit /b
  54. ::=====================HEX→ASCII=========================================
  55. :file2asc
  56. set "file_size=%~z1"
  57. ::计算文件有效行数
  58. for /f %%a in ('findstr/n . "%~1"^|find /c /v ""') do set /a line=%%a
  59. ::计算生成文件Byte数 (%file_size% - %line%*2回车换行符 + %line%*1个空格)/3
  60. set /a dec_size=(%file_size%-%line% )/3
  61. call:dec2hex %dec_size%
  62. set dec_size_HEX=%sum%
  63. set /a end_addr=%dec_size%+0x100
  64. call:dec2hex %end_addr%
  65. set end_addr=%sum%
  66. echo 源文件:   行数:%line% 字节数:%file_size%
  67. echo 目标文件: 字节数:%dec_size% == 0x%dec_size_HEX%
  68. echo. -----------------------------------------------
  69. ::做个判断标记,用于%hex_str%写入文件控制.
  70. if %file_size% GTR 8160 (set BigFile_BL=1&cd.>tmpHEX.txt) else (set BigFile_BL=0)
  71. if %BigFile_BL%==1 (
  72. set /a _addr=100
  73. set "YU_str="
  74. for /f "tokens=* usebackq delims=" %%a in ("%~1") do (
  75. if "!YU_str!"=="" (set str=%%a) else (set str=!YU_str! %%a)
  76. call:getStrLen "!str!"
  77. set /a s=0
  78. set /a count=^(!strlen!+1^) / 72
  79. set /a YU_str=^(!strlen!+1^) "%%" 72
  80. if !YU_str! EQU 0 (set "YU_str=") else (call set "YU_str=%%str:~-!YU_str!%%")
  81. for /l %%i in (1,1,!count!) do (
  82. call echo e!_addr! %%str:~!s!,72%%>>tmpHEX.txt
  83. set /a s=%%i*72
  84. set /p = e!_addr!<nul
  85. set /a _addr=0x!_addr! + 24
  86. call:dec2hex !_addr!
  87. set _addr=!sum!
  88. )
  89. )
  90. call echo e!_addr! %%str:~!s!%%>>tmpHEX.txt
  91. set /p = e!_addr!<nul
  92.     (echo n%~s2&echo rcx&echo %dec_size_HEX%&echo w&echo q)>>tmpHEX.txt
  93. rem (echo n%~s2&echo rcx&echo %dec_size_HEX%&echo w&echo d100 %end_addr%&echo q)>>tmpHEX.txt
  94. debug<tmpHEX.txt>nul
  95. goto:eof
  96. )
  97. ::--------------------------------------------------------------
  98. if "%line%"=="1" (
  99. rem set /p hex_str=<"%~1"
  100. rem 上面命令单行文本(>1022 Byte)并不能读完
  101. for /f "tokens=* usebackq delims=" %%a in ("%~1") do set "hex_str=%%a"
  102. ) else (
  103. for /f "tokens=* usebackq delims=" %%a in ("%~1") do set "hex_str=!hex_str! %%a"
  104. )
  105. ::(echo e100 %hex_str%&echo n%~s2&echo rcx&echo %dec_size_HEX%&echo w&echo q)|debug>nul
  106. (echo e100&echo %hex_str%&echo n%~s2&echo rcx&echo %dec_size_HEX%&echo w&echo q)|debug>nul
  107. ::(echo e100&echo %hex_str%&echo n%~s2&echo rcx&echo %dec_size_HEX%&echo w&echo q)|debug>strHEX.txt
  108. ::echo exit|%ComSpec% /k prompt e100$_%hex_str%$_$_$_n%~s2$_rcx$_%sum%$_w$_q$_|debug>strHEX.txt
  109. ::echo exit|%ComSpec% /k prompt e100 %hex_str%$_n%~s2$_rcx$_%sum%$_w$_q$_|debug>strHEX.txt
  110. goto:eof
  111. ::=======================10进制转16进制==============================
  112. ::返回值在中%sum%
  113. :dec2hex decNumber
  114. set "str=0123456789ABCDEF"&set "sum="
  115. set/a d=%~1
  116. for /l %%i in (1 1 8) do (
  117.      if !d! GTR 0 (
  118.         set/a "y=d&15,d>>=4"
  119.         call set sum=%%str:~!y!,1%%!sum!
  120.      )
  121. )
  122. if not defined sum set/a sum=0
  123. goto:eof
  124. ::==============END======10进制转16进制==============================
  125. ::=======================获得字符串长度==============================
  126. ::返回值在中%strlen%
  127. :getStrLen mystr
  128. set "mystr=%~1"&set "strlen=0"
  129. if not defined mystr goto :eof
  130. for /l %%i in (0,1,8191) do (set strlen=%%i&if "!mystr:~%%i,1!"=="" (goto :eof))
  131. goto :eof
  132. ::==============END======获得字符串长度========== by hf-g ===========
复制代码

作者: ivor    时间: 2012-2-3 17:17

支持一下




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2