<http://www.netikka.net/tsneti/info/tscmd073.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Tue 27-Apr-2010 19:14:24

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



73} How can I convert a hexadecimal string into an ASCII string?

A Visual Basic Script (VBScript) aided command line script solution:
  @echo off & setlocal enableextensions
  ::
  set HexString=48 65 6C 6C 6F 20 77 6F 72 6C 64 21
  ::
  :: Build a Visual Basic Script

  set vbs_=%temp%\hexdec.vbs
  set skip=
  findstr "'%skip%VBS" "%~f0" > "%vbs_%"
  :: Run it with Microsoft Windows Script Host Version 5.6
  for /f "tokens=* delims=" %%a in (
    'cscript //nologo "%vbs_%" "%HexString%"') do set AsciiString=%%a
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist %%f del %%f
  ::
  :: Show the result

  echo %HexString%
  echo %AsciiString%
  endlocal & goto :EOF
  '
  '................................................................
  'The Visual Basic Script

  set arg = WScript.Arguments 'VBS
  WScript.Echo HexStringToAsciiString(arg(0)) 'VBS
  '
  Function HexStringToAsciiString(HexStr) 'VBS
    For i = 1 To Int(Len(HexStr)/3)+1 'VBS
      str1 = str1 & Chr("&H" & Mid(HexStr,3*i-2,2)) 'VBS
    Next 'VBS
    HexStringToAsciiString=str1 'VBS
  End Function 'VBS

The output will be
  D:\TEST>cmdfaq
  48 65 6C 6C 6F 20 77 6F 72 6C 64 21
  Hello world!

The VBS logic written slightly differently:
  @echo off & setlocal enableextensions
  ::
  set HexString=48 65 6C 6C 6F 20 77 6F 72 6C 64 21
  ::
  :: Build a Visual Basic Script

  if not exist c:\mytemp mkdir c:\mytemp
  findstr "'%skip%VBS" "%~f0" > c:\mytemp\tmp$$$.vbs
  :: Run it with Microsoft Windows Script Host Version 5.6
  cscript //nologo c:\mytemp\tmp$$$.vbs>c:\mytemp\tmp$$$.cmd
  ::
  :: Call the CMD script made by the VBS script
  call c:\mytemp\tmp$$$.cmd
  ::
  :: Clean up

  for %%f in (c:\mytemp\tmp$$$.cmd c:\mytemp\tmp$$$.vbs) do del %%f
  rmdir c:\mytemp
  ::
  :: Show the result

  echo %HexString%
  echo %AsciiString%
  endlocal & goto :EOF
  '
  '................................................................
  'The Visual Basic Script

  Dim MyHexString 'VBS
  Set WshShell = WScript.CreateObject("WScript.shell") 'VBS
  MyHexString = WshShell.ExpandEnvironmentStrings("%HexString%") 'VBS
  WScript.Echo "@set AsciiString=" & HexStringToAsciiString(MyHexString) 'VBS
  '
  Function HexStringToAsciiString(HexStr) 'VBS
    For i = 1 To Int(Len(HexStr)/3)+1 'VBS
      str1 = str1 & Chr("&H" & Mid(HexStr,3*i-2,2)) 'VBS
    Next 'VBS
    HexStringToAsciiString=str1 'VBS
  End Function 'VBS