JSI Tip 10609. How can I return the hexadecimal representation of a string?

I have scripted Str2Hex.bat to return the hexadecimal representation of a string.

The syntax for using Str2Hex.bat is:

call Str2Hex String HexString


String    is the string you wish to represent in hexadecimal.

HexString is a call directed environment variable that will contain hexadecimal representation of String.


call str2hex "Hello World" hw

Typing @echo %hw% displays 48656C6C6F20576F726C64

Str2Hex.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax: Str2Hex String HexString&goto :EOF
if exist "%TEMP%\Str2Hex.vbs" goto cvt
@echo.dim  objArguments>"%TEMP%\Str2Hex.vbs"
@echo.set objArguments = Wscript.Arguments>>"%TEMP%\Str2Hex.vbs"
@echo.For i = 1 To Len(str)>>"%TEMP%\Str2Hex.vbs"
@echo. hx=hx ^&  Right("0" ^& hex(CStr(Asc(Mid(str, i, 1)))), 2)>>"%TEMP%\Str2Hex.vbs"
@echo.Wscript.Echo hx>>"%TEMP%\Str2Hex.vbs"
for /f "Tokens=*" %%x in ('cscript //nologo "%TEMP%\Str2Hex.vbs" %1') do (
 set %2=%%x

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.