标题: [文本处理] 批处理版HexFile2Ascii [打印本页]
作者: hfg1977 时间: 2012-2-3 04:54 标题: 批处理版HexFile2Ascii
不解释了,Debug用法google吧,很多的.- @goto start
- :help
- @echo.======== Ver:1.0 ==================== 发布日期: 2012-02-03 ==============
- @echo. BAT_NAME: HexFile2Ascii.bat
- @echo. 返回值: %def_filename%
- @echo. 返回值获得: ┌ set /p strHEX=^<%def_filename%
- @echo. └ echo %%strHEX%%
- @echo. 调用: call HexFile2Ascii.bat HexFileName [outFileName]
- @echo. 说明:
- @echo. 本批处理是从Ascii2Hex.bat中分离出来的,默认系统XP.
- @echo. 转载必须指明出处: "转载自批处理之家 bbs.bathome.net"
- @echo. HexFile必须是 "0E 02 56 FE" 形式,以空格分隔
- @echo. 使用第三方工具sed对原始文件预处理,如果没有则跳过该过程.
- @echo. 大小限制: ASCII文件 65279 Byte HEX文件65279*3=195837 Byte
- @echo.
- @echo.====================== Copyright@ by hf-g [彭城] ========================
- ping /n 4 127.0>nul&
- goto:end
- ::==============================================================
- :start
- @echo off
- setlocal enabledelayedexpansion
- ::==============================================================
- set "def_filename=ret_str.txt"
- if /i "%~1"=="" goto help
- if /i "%~1"=="-?" goto help
- 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
- ::工作路径设置:
- (pushd %~dp1&&pushd %~dp2&&pushd %~dp3&&pushd %~dp4)>nul
- echo CD=%cd%
- set "char="&set "filename=nul"&set "dec_size="&set "hex_str="&set "ascChar="&set "des_filename=nul"
- set /a char_BL=0,filename_BL=0,asc2hex_BL=0,hex2asc_BL=0,IPadd_BL=0,exit_BL=0
- set /a des_filename_BL=0
- set /a line=0,line_YU=0,count=0,num=0
-
- ::================================ Mian ==============================
- path=%path%;%~d0\DOS\网络;%~d0\DOS\command\第三方命令行程序;%~d0\DOS\command;%~d0\DOS\源码;%~d0\DOS\sysbkup
- if not "%~2"=="" set "def_filename=%~2"
- if not exist "%~1\." (if not exist "%~1" (echo ERR:Hex文件不存在&goto:end))
-
- ::用第三方工具sed对原始文件预处理,如果没有则跳过该过程. 文件给予备份.
- sed --v 1>nul 2>nul&&(
- if not exist "%~nx1.bak" copy "%~nx1" "%~nx1.bak"
- sed -i "s/\s*$//;s/^\s]*//;/^$/d" "%~1"
- )||(
- echo 第三方工具 sed 没找到.&echo........
- )
-
- call:file2asc "%~1" "%def_filename%"
-
- :end
- pause&pause
- ::echo ☆☆☆
- del tempfile.txt strHEX.txt tmpHEX.txt>nul 2>nul
- del sed??????
- popd&popd&popd&popd
- exit /b
-
- ::=====================HEX→ASCII=========================================
- :file2asc
- set "file_size=%~z1"
- ::计算文件有效行数
- for /f %%a in ('findstr/n . "%~1"^|find /c /v ""') do set /a line=%%a
-
- ::计算生成文件Byte数 (%file_size% - %line%*2回车换行符 + %line%*1个空格)/3
- set /a dec_size=(%file_size%-%line% )/3
- call:dec2hex %dec_size%
- set dec_size_HEX=%sum%
- set /a end_addr=%dec_size%+0x100
- call:dec2hex %end_addr%
- set end_addr=%sum%
-
- echo 源文件: 行数:%line% 字节数:%file_size%
- echo 目标文件: 字节数:%dec_size% == 0x%dec_size_HEX%
- echo. -----------------------------------------------
-
-
- ::做个判断标记,用于%hex_str%写入文件控制.
- if %file_size% GTR 8160 (set BigFile_BL=1&cd.>tmpHEX.txt) else (set BigFile_BL=0)
- if %BigFile_BL%==1 (
- set /a _addr=100
- set "YU_str="
- for /f "tokens=* usebackq delims=" %%a in ("%~1") do (
- if "!YU_str!"=="" (set str=%%a) else (set str=!YU_str! %%a)
- call:getStrLen "!str!"
- set /a s=0
- set /a count=^(!strlen!+1^) / 72
- set /a YU_str=^(!strlen!+1^) "%%" 72
- if !YU_str! EQU 0 (set "YU_str=") else (call set "YU_str=%%str:~-!YU_str!%%")
-
- for /l %%i in (1,1,!count!) do (
- call echo e!_addr! %%str:~!s!,72%%>>tmpHEX.txt
- set /a s=%%i*72
- set /p = e!_addr!<nul
- set /a _addr=0x!_addr! + 24
- call:dec2hex !_addr!
- set _addr=!sum!
- )
- )
- call echo e!_addr! %%str:~!s!%%>>tmpHEX.txt
- set /p = e!_addr!<nul
- (echo n%~s2&echo rcx&echo %dec_size_HEX%&echo w&echo q)>>tmpHEX.txt
- rem (echo n%~s2&echo rcx&echo %dec_size_HEX%&echo w&echo d100 %end_addr%&echo q)>>tmpHEX.txt
- debug<tmpHEX.txt>nul
- goto:eof
- )
- ::--------------------------------------------------------------
-
- if "%line%"=="1" (
- rem set /p hex_str=<"%~1"
- rem 上面命令单行文本(>1022 Byte)并不能读完
- for /f "tokens=* usebackq delims=" %%a in ("%~1") do set "hex_str=%%a"
- ) else (
- for /f "tokens=* usebackq delims=" %%a in ("%~1") do set "hex_str=!hex_str! %%a"
- )
- ::(echo e100 %hex_str%&echo n%~s2&echo rcx&echo %dec_size_HEX%&echo w&echo q)|debug>nul
- (echo e100&echo %hex_str%&echo n%~s2&echo rcx&echo %dec_size_HEX%&echo w&echo q)|debug>nul
- ::(echo e100&echo %hex_str%&echo n%~s2&echo rcx&echo %dec_size_HEX%&echo w&echo q)|debug>strHEX.txt
- ::echo exit|%ComSpec% /k prompt e100$_%hex_str%$_$_$_n%~s2$_rcx$_%sum%$_w$_q$_|debug>strHEX.txt
- ::echo exit|%ComSpec% /k prompt e100 %hex_str%$_n%~s2$_rcx$_%sum%$_w$_q$_|debug>strHEX.txt
- goto:eof
- ::=======================10进制转16进制==============================
- ::返回值在中%sum%
- :dec2hex decNumber
- set "str=0123456789ABCDEF"&set "sum="
- set/a d=%~1
- for /l %%i in (1 1 8) do (
- if !d! GTR 0 (
- set/a "y=d&15,d>>=4"
- call set sum=%%str:~!y!,1%%!sum!
- )
- )
- if not defined sum set/a sum=0
- goto:eof
- ::==============END======10进制转16进制==============================
- ::=======================获得字符串长度==============================
- ::返回值在中%strlen%
- :getStrLen mystr
- set "mystr=%~1"&set "strlen=0"
- if not defined mystr goto :eof
- for /l %%i in (0,1,8191) do (set strlen=%%i&if "!mystr:~%%i,1!"=="" (goto :eof))
- goto :eof
- ::==============END======获得字符串长度========== by hf-g ===========
复制代码
作者: ivor 时间: 2012-2-3 17:17
支持一下
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |