2016-01-29 108 views
5

DOSのバッチスクリプトでsringを16進数に変換するにはどうすればよいですか? たとえば、「abcd」を「61626364」に変換します。 'a'は0x61 ...DOSのバッチスクリプトで文字列2の16進数を変換する

私はウェブから解決策を見つけようとしましたが、私の答えは見つかりませんでした。

+0

。それがオプションの場合、PowerShellで簡単になるでしょう。 –

答えて

3
:stringToHex 
@echo off 
del tmp.hex >nul 2>nul 
del tmp.str >nul 2>nul 
if "%~1" equ "" (
    echo no string passed 
    exit /b 1 
) 
echo|set /p=%~1 >tmp.str 
::(echo(%~1)>tmp.str 
rem certutil -dump tmp.str 
certutil -encodehex tmp.str tmp.hex >nul 
setlocal enableDelayedExpansion 
set "hex_str=" 
for /f "usebackq tokens=2 delims= " %%A in ("tmp.hex") do (
    set "line=%%A" 
    set hex_str=!hex_str!!line:~0,48! 
    set hex_str=!hex_str: =! 

) 
set hex_str=%hex_str:~0,-2% 
echo !hex_str! 

!! stackoverflowエディタがタブ文字を壊している可能性があります。 tokens=2 delims= " delimsの後にはTABという単一の文字が必要です。

には引数として渡される文字列が必要です.certutilの代わりに使用できるdebnhamの:hexDump関数も見てください。

5
@echo off 
setlocal EnableDelayedExpansion 

rem Store the string in chr.tmp file 
set /P "=%~1" <NUL> chr.tmp 

rem Create zero.tmp file with the same number of Ascii zero characters 
for %%a in (chr.tmp) do fsutil file createnew zero.tmp %%~Za > NUL 

rem Compare both files with FC /B and get the differences 
set "hex=" 
for /F "skip=1 tokens=2" %%a in ('fc /B chr.tmp zero.tmp') do set "hex=!hex!%%a" 

del chr.tmp zero.tmp 
echo %hex% 

出力例:私はバッチファイルとソリューションがある疑う

C:\> test.bat abcd 
61626364 
+0

クール:-)、certutilより簡単です。 – npocmaka

関連する問題