String DOS批处理脚本,用于转换字符串2十六进制

String DOS批处理脚本,用于转换字符串2十六进制,string,batch-file,hex,character,dos,String,Batch File,Hex,Character,Dos,如何在DOS批处理脚本中将sring转换为十六进制? 例如,将“abcd”转换为“61626364”。 因为“a”是0x61 一天,我试图从网上找到一个解决方案,但找不到我的答案 :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 >t

如何在DOS批处理脚本中将sring转换为十六进制? 例如,将“abcd”转换为“61626364”。 因为“a”是0x61

一天,我试图从网上找到一个解决方案,但找不到我的答案

: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


需要作为参数传递的字符串。还可以查看debnham的函数,您可以使用该函数来代替certutil。

我怀疑是否有一个仅使用批处理文件的解决方案。如果这是一个选项,PowerShell将很容易实现。
@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