Ascii与HEX 文件 字符 的相互转换
文件下载: MD5: 837efc2b409db1d9ae2854d07857d16f复制代码
- @goto start
- :help
- @echo.======== Ver:2.0 ==================== 发布日期: 2010-08-09 ==============
- @echo. BAT_NAME: Ascii2Hex.bat
- @echo. 返回值: %def_filename%
- @echo. 返回值获的: ┌ set /p strHEX=^<%def_filename%
- @echo. └ echo %%strHEX%%
- @echo. 调用: call ASCII_Hex.bat [[-c] char] [-f filename] -A -H -?
- @echo. -c char : 字符 (默认)
- @echo. -f filename : 文件
- @echo. -A : ASCII 转换到 HEX (默认)
- @echo. -H : HEX 转换到 ASCII
- @echo. -X : 转换到网址方式(x:\abc\%%B2%%CE\1.jpg)
- @echo. -? : 查看帮助
- @echo. 例如: call Ascii2Hex.bat abc def
- @echo. call Ascii2Hex.bat "abc def"
- @echo. 此时 "" 也算作字符,必须成对使用
- @echo. call Ascii2Hex.bat -f filename -f filename -X
- @echo. 此时 前一个filename作为源文件,后一个为目标文件
- @echo. call Ascii2Hex.bat -c "abc def" -a
- @echo. -H [-c char] [-f filename]
- @echo. 字符必须是 "0E 02 56 FE" 形式,以空格分隔
- @echo. 大小限制: ASCII文件 65279 Byte HEX文件65279*3=195837 Byte
- echo --^<MORE^>--&pause>nul
- @echo. 下列命令格式是正确的:
- @echo. call Ascii2Hex.bat -x -f 123.txt
- @echo. call Ascii2Hex.bat -f "12 3.txt" -x -f c:\x
- @echo. call Ascii2Hex.bat -f 2 abc def love
- @echo.====================== Copyright@ by hf-g 2010-07-15 [彭城] =============
- ping /n 4 127.0>nul&
- goto:end
- ::==============================================================
- :start
- @echo off
- setlocal enabledelayedexpansion
- ::==============================================================
- set "def_filename=ret_str.txt"
- ::echo "%1" & pause
- 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
- call:get_P %*
- if "%exit_BL%"=="1" goto end
- if not "%hex2asc_BL%"=="1" (
- if "!char!"=="" (
- if not exist "%filename%" (echo 文件未找到 &set /a exit_BL=1&goto end)
- call:file2hex "%filename%"
- if "%des_filename_BL%"=="1" (
- echo. ① ASCII文件 → HEX文件 &@echo. 文件 = %filename% → %des_filename%
- if "!BigFile_BL!"=="1" (echo !hex_str!>>"%def_filename%"&ren "%def_filename%" "%des_filename%") else (echo !hex_str!>"%des_filename%")
- ) else (
- echo. ② ASCII文件 → HEX文件 &@echo. 文件 = %filename% → %def_filename%
- if "!BigFile_BL!"=="1" (echo !hex_str!>>"%def_filename%") else (echo !hex_str!>"%def_filename%")
- )
- ) else (
- call:char2hex %char%
- if "%filename_BL%"=="1" (
- echo. ③ ASCII字符 → HEX文件 &@echo. 字符 = %char% → %filename%
- echo !hex_str!>"%filename%"
- ) else (
- echo. ④ ASCII字符 → HEX字符 &@echo. 字符 = %char% → %def_filename%
- echo !hex_str!>"%def_filename%"
- )
- )
- ) else (
- if "!char!"=="" (
- if not exist "%filename%" (echo 文件未找到 &set /a exit_BL=1&goto end)
- if "%des_filename_BL%"=="1" (
- echo. ⑤ HEX文件 → ASCII文件 &@echo. 文件 = %filename% → %des_filename%
- call:file2asc "%filename%" "%des_filename%"
- ) else (
- echo. ⑥ HEX文件 → ASCII文件 &@echo. 文件 = %filename% → %def_filename%
- call:file2asc "%filename%" "%def_filename%"
- )
- ) else (
- set /p=%char%<nul>tempfile.txt
- if "%filename_BL%"=="1" (
- echo. ⑦ HEX字符 → ASCII文件 &@echo. 字符 = %char% → %filename%
- call:file2asc "tempfile.txt" "%filename%"
- set /p hex_str=<"%filename%"
- echo %hex_str%
- ) else (
- echo. ⑧ HEX字符 → ASCII字符 &@echo. 字符 = %char% → %def_filename%
- call:file2asc "tempfile.txt" %def_filename%
- set /p hex_str=<"%def_filename%"
- echo !hex_str!
- )
- )
- )
- :end
- ::echo ☆☆☆
- del tempfile.txt strHEX.txt>nul 2>nul
- popd&popd&popd&popd
- exit /b
- ::=====================ASCII→HEX=========================================
- :char2hex
- echo %char%>tempfile.txt
- call:file2hex tempfile.txt char
- goto:eof
- ::=====================ASCII→HEX=========================================
- :file2hex
- set "dec_size=%~z1"
- if "%~2"=="char" (set /a dec_size-=2)
- ::转换16进制
- call:dec2hex %dec_size%
- echo. -----------------------------------------------
- echo. 共计: %dec_size% == 0x%sum% byte
- ::计算debug生成文件的行数
- set /a line=%dec_size% / 16
- set /a line_YU=%dec_size% %% 16
- if %line_YU% GEQ 1 (set /a line+=1)
- set "sum=%sum:~0,-1%F"
- echo. Debug_addr:%sum% 行数:%line%
- ::WinXP+命令行字符串最长8191个字符,当%hex_str%=8160(即源文件 2720&hAA0 BYte)字符时跳出for循环.
- ::2720/16=170行
- ::做个判断标记,用于%hex_str%写入文件控制.
- if %dec_size% GTR 2720 (set BigFile_BL=1&cd.>"%def_filename%") else (set BigFile_BL=0)
- ::地址+&H100
- ::set /a sum=0x%sum%+0x100
- ::call:dec2hex %sum%
- if "%~2"=="char" (
- (echo f0 %sum% 0&echo a0&echo DB '%char%'&echo. &echo d0 %sum%&echo q)|debug>strHEX.txt
- rem echo exit|%ComSpec% /k prompt f0 %sum% 0$_a0$_DB '%char%'$_$_d0 %sum%$_q$_|debug>strHEX.txt
- ) else (
- rem echo exit|%ComSpec% /k prompt d100 %sum%$_q$_|debug %~s1>strHEX.txt
- echo exit|%ComSpec% /k prompt f0 %sum% 0$_n%~s1$_l0$_d0 %sum%$_q$_|debug>strHEX.txt
- )
- for /f "tokens=1 delims=:" %%a in ('findstr -n "^-d" strHEX.txt') do set "skip=%%a"&&goto :f2h_p
- :f2h_p
- ::13D3:0000 47 3A 5C 44 4F 53 5C D4-B4 C2 EB 20 31 32 33 5C G:\DOS\.... 123\
- ::01234567890123456789012345678901234567890123456789012345678901234567890123456
- ::Debug 一行共计77个字符(9+2+16*3+2+16=77),s_Char=前8个字节,e_Char=后8个字节
- for /f "skip=%skip% tokens=* usebackq delims=" %%a in ("strHEX.txt") do (
- set /a count=!count!+1
- set "allChar=%%a"
- set "s_Char=!allChar:~11,23!"
- set "e_Char=!allChar:~35,23!"
- set "ascChar=!allChar:~61,16!"
- if "%IPadd_BL%"=="1" (
- if "0x!s_Char:~0,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~0,1!") else (set "hex_str=!hex_str!%%!s_Char:~0,2!")
- if "0x!s_Char:~3,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~1,1!") else (set "hex_str=!hex_str!%%!s_Char:~3,2!")
- if "0x!s_Char:~6,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~2,1!") else (set "hex_str=!hex_str!%%!s_Char:~6,2!")
- if "0x!s_Char:~9,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~3,1!") else (set "hex_str=!hex_str!%%!s_Char:~9,2!")
- if "0x!s_Char:~12,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~4,1!") else (set "hex_str=!hex_str!%%!s_Char:~12,2!")
- if "0x!s_Char:~15,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~5,1!") else (set "hex_str=!hex_str!%%!s_Char:~15,2!")
- if "0x!s_Char:~18,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~6,1!") else (set "hex_str=!hex_str!%%!s_Char:~18,2!")
- if "0x!s_Char:~21,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~7,1!") else (set "hex_str=!hex_str!%%!s_Char:~21,2!")
- if "0x!e_Char:~0,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~8,1!") else (set "hex_str=!hex_str!%%!e_Char:~0,2!")
- if "0x!e_Char:~3,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~9,1!") else (set "hex_str=!hex_str!%%!e_Char:~3,2!")
- if "0x!e_Char:~6,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~10,1!") else (set "hex_str=!hex_str!%%!e_Char:~6,2!")
- if "0x!e_Char:~9,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~11,1!") else (set "hex_str=!hex_str!%%!e_Char:~9,2!")
- if "0x!e_Char:~12,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~12,1!") else (set "hex_str=!hex_str!%%!e_Char:~12,2!")
- if "0x!e_Char:~15,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~13,1!") else (set "hex_str=!hex_str!%%!e_Char:~15,2!")
- if "0x!e_Char:~18,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~14,1!") else (set "hex_str=!hex_str!%%!e_Char:~18,2!")
- if "0x!e_Char:~21,2!" lss "0x7f" (set "hex_str=!hex_str!!ascChar:~15,1!") else (set "hex_str=!hex_str!%%!e_Char:~21,2!")
- ) else (
- if "%BigFile_BL%"=="0" (
- set "hex_str=!hex_str!!s_Char! !e_Char! "
- ) else (
- set "hex_str=!hex_str!!s_Char! !e_Char! "
- set /a write_BL=!count! "%%" 150
- if !count! GEQ %line% goto jmp1
- if !write_BL! EQU 0 (echo !hex_str:~0,-1!>>"%def_filename%"&set "hex_str=")
- )
- )
- set /p = +!count!+%line%+<nul
- if !count! GEQ %line% goto jmp1
- )
- :jmp1
- if "%IPadd_BL%"=="1" (
- set /a line_YU=16-!line_YU!
- if not "!line_YU!"=="16" (call set "hex_str=%%hex_str:~0,-!line_YU!%%")
- ) else (
- rem 计算byte,一个字符占3格(+1个空格)
- if not "!line_YU!"=="0" (set /a line_YU=^(16-!line_YU!^)*3+1) else (set /a line_YU=1)
- call set "hex_str=%%hex_str:~0,-!line_YU!%%")
- )
- if "%~2"=="char" (echo 返回值=%hex_str%)
- ::if !line_YU! == 16 set /a line_YU=0
- goto:eof
- ::=====================HEX→ASCII=========================================
- :file2asc
- set "file_size=%~z1"
- ::文件如果是单行的 多行的
- ::计算文件有效行数
- for /f %%a in ('findstr/n . "%~1"^|find /c /v ""') do set /a line=%%a
- ::echo 行数:%line%&pause
- ::prompt命令本身有长度限制1024 Byte,所以总命令行不能太长.
- ::计算生成文件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. -----------------------------------------------
- ::--------------------------大文件处理--------------------------------------------------------------------
- ::WinXP+命令行字符串最长8191个字符,当%hex_str%=8160(即源文件 2720&hAA0 Byte)字符时跳出for循环.
- ::2720/16=170行 8160/3=2720
- ::当地址大于FFFF时,会重新回到当前段的0000地址.解决方法:指定段址:地址,地址大于FFFF时段址+1.??
- ::ASCII文件FFFF-H100=65279 Byte HEX文件65279*3=195837
- ::做个判断标记,用于%hex_str%写入文件控制.
- if %file_size% GTR 8160 (set BigFile_BL=1&cd.>tmpHEX.txt) else (set BigFile_BL=0)
- ::if %file_size% GTR 195837 (echo 文件大于64K,不可处理.&goto:eof)
- ::以24字节为一行,原始文件:16*3*21=1008-空格=1007每行,比较美观
- ::一次读取的字节越多,工作速度越快
- 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
- del tmpHEX.txt 2>nul
- goto:eof
- )
- ::--------------------end-------大文件处理----------------------------------------------------------------
- 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
- ::========================参数设置与获得============================
- :get_p
- ::echo ①"%~1" & pause
- if /i "%~1"①=="-c"① (
- set "char=%~2"&set /a char_BL=1 & goto get_p3
- ) else (
- if /i "%~1"=="-f" (
- set "filename=%~2"&set /a filename_BL=1 & goto get_p3
- ) else (
- if /i "%~1"=="-A" (
- set /a asc2hex_BL=1
- ) else (
- if /i "%~1"=="-H" (
- set /a hex2asc_BL=1
- ) else (
- if /i "%~1"=="-X" (
- set /a IPadd_BL=1
- ) else (
- rem 如果%1是个文件而不是文件夹
- if not exist "%~1.\" (
- if exist "%~1" (set "filename=%~1"&set /a filename_BL=1 & goto get_p_end)
- )
- set "char=%*" & goto get_p_end
- )
- )
- )
- )
- )
- :get_p2
- ::echo ②"%~1" & pause
- if /i "%~2"=="" goto:eof
- if /i "%~2"②=="-c"② (
- set "char=%~3"&set /a char_BL=1 & goto get_p4
- ) else (
- if /i "%~2"=="-f" (
- rem: [-AHX] [-f filename]
- set "filename=%~3"&set /a filename_BL=1 & goto get_p4
- ) else (
- if /i "%~2"=="-A" (
- set /a asc2hex_BL=1
- ) else (
- if /i "%~2"=="-H" (
- set /a hex2asc_BL=1
- ) else (
- if /i "%~2"=="-X" (
- set /a IPadd_BL=1
- ) else (
- rem: [-AHX] char
- set "char=%*"
- set "char=!char:~3!"
- if "%hex2asc_BL%"=="1" set "char=!char:"=!"
- goto get_p_end
- )
- )
- )
- )
- )
- :get_p3
- if /i "%~3"=="" goto:eof
- if /i "%~3"③=="-c"③ (
- rem: [-f filename] -C char
- set "char=%~4"&set /a char_BL=1 & goto get_p5
- ) else (
- if /i "%~3"=="-f" (
- rem: [-f filename] -f filename
- rem: [-c char] -f filename
- if "%filename_BL%"=="1" (set "des_filename=%~4"&set /a des_filename_BL=1 & goto get_p5)
- if "%filename_BL%"=="0" (set "filename=%~4"&set /a filename_BL=1 & goto get_p5)
- ) else (
- if /i "%~3"=="-A" (
- set /a asc2hex_BL=1
- ) else (
- if /i "%~3"=="-H" (
- set /a hex2asc_BL=1
- ) else (
- if /i "%~3"=="-X" (
- set /a IPadd_BL=1
- ) else (
- rem: [-f filename] char
- rem: [-c char] char
- set "char=%*"
- if "%char_BL%"=="1" (set "char=!char:~3!") else (set "char=!char:%~1 %~2 =!")
- goto get_p_end
- )
- )
- )
- )
- )
- )
- :get_p4
- if /i "%~4"=="" goto:eof
- if /i "%~4"④=="-c"④ (
- set "char=%~5"&set /a char_BL=1 & goto get_p_end
- ) else (
- if /i "%~4"=="-f" (
- rem: [-AHX] [-f filename] -f filename
- rem: [-f filename] [-AHX] -f filename
- if "%filename_BL%"=="1" (set "des_filename=%~5"&set /a des_filename_BL=1 & goto get_p_end)
- ) else (
- if /i "%~4"=="-A" (
- set /a asc2hex_BL=1
- ) else (
- if /i "%~4"=="-H" (
- set /a hex2asc_BL=1
- ) else (
- if /i "%~4"=="-X" (
- set /a IPadd_BL=1
- ) else (
- rem: [-AHX] [-f filename] char
- set "char=%*"
- set "char=!char:%~1 %~2 %~3=!" & goto get_p_end
- )
- )
- )
- )
- )
- )
- :get_p5
- if /i "%~5"=="" goto:eof
- rem: [-f filename] [-f filename] [-AHX]
- rem: [-c char] [-f filename] [-AHX]
- if /i "%~5"⑤=="-c"⑤ (
- echo get_p5 error: "-c" &goto end
- ) else (
- if /i "%~5"=="-f" (
- echo get_p5 error: "-c" &goto end
- ) else (
- if /i "%~5"=="-A" (
- set /a asc2hex_BL=1
- ) else (
- if /i "%~5"=="-H" (
- set /a hex2asc_BL=1
- ) else (
- if /i "%~5"=="-X" (
- set /a IPadd_BL=1
- ) else (
- echo get_p5 error: 参数5 &set /a exit_BL=1&goto end
- )
- )
- )
- )
- )
- )
- :get_p_end
- goto:eof
- ::=======================10进制转16进制==============================
- ::返回值在中%sum%
- :dec2hex decNumber
- if not "%frist_BL%"=="1" (set "frist_BL=1"&set "sum="&set "yu_num="&set /a sor_num=%*)
- set /a yu_num= %sor_num% %% 16
- set /a sor_num /= 16
- call:set_YU %yu_num%
- set sum=%ret%%sum%
- if %sor_num% lss 16 (set "end_BL=1"&call:set_YU %sor_num%) else (set "end_BL=0")
- if "%end_BL%"=="1" (set sum=%ret%%sum%&set "frist_BL=0"&goto:eof)
- goto dec2hex
- :set_YU
- set ret=
- if "%1" == "10" set ret=A
- if "%1" == "11" set ret=B
- if "%1" == "12" set ret=C
- if "%1" == "13" set ret=D
- if "%1" == "14" set ret=E
- if "%1" == "15" set ret=F
- if %1 lss 10 set ret=%1
- goto :eof
- ::==============END======10进制转16进制========== by hf-g ===========
- ::=======================获得字符串长度==============================
- ::返回值在中%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 ===========
复制代码
- ::=======================10进制转16进制==============================
- ::返回值在中%sum%
- :dec2hex decNumber
- set str=0123456789ABCDEF&set "sum="
- set/a d=%~1
- :lp
- set/a y=!d!%%16,d/=16
- call set sum=%%str:~!y!,1%%!sum!
- if !d! NEQ 0 goto:lp
- goto :eof
- ::==============END======10进制转16进制==============================
欢迎光临 批处理之家 (http://bbs.bathome.net/) | Powered by Discuz! 7.2 |