Convert decimal to hex, and bytes to chars

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Convert decimal to hex, and bytes to chars

#1 Post by jeb » 09 Apr 2010 11:56

Hi,

for those who are interested.

Three functions (perhaps anyone find it usefull).

    :dec2hex <hexResult> <decimalValue> <minimumDigits>
    :byteList2string <resultChar> <byte-1> [<byte-2> ...<byte-n>]
    :NormalizeVariable <varName>

:dec2hex
Converts a number to hex

Code: Select all

call :dec2hex hexResult 4079 4
echo 0x%hexResult%

Output: 0x100A


Convert one or more bytes into a string

Code: Select all

call :byteList2string strResult 65 66 67
echo %strResult%

Output: ABC


This is usefull for get special characters like CR(13), or BACKSPACE(8), or BELL(7)
:!: To print a CR haracters use delayed expansion, because with %var% it does not work

Code: Select all

call :byteList2string char7 7
call :byteList2string char8 8
call :byteList2string char13 13
echo ABC%char7%XYZ
echo ABC%char8%XYZ
echo ABC!char13!XYZ%char13%-
echo\


Output:
ABCXYZ      (Beep)
ABXYZ        (move to the C and overwrite it)
XYZ-          (move to the front and overwrite ABC)


The last function is a bit complicated, particulary the intention. :wink:
I use it, if I want to create a temporary file, and I want to be independent, if temp (with spaces) contains or not contains quotes.

Code: Select all

set temp1=C:\my temp
set temp2="C:\my temp"
call :NormalizeVariable temp1
call :NormalizeVariable temp2
echo temp1=%temp1%
echo temp2=%temp2%

Output:
temp1=C:\my temp
temp2=C:\my temp


Code: Select all

@ECHO  off
setlocal EnableDelayedExpansion
call :NormalizeVariable temp

echo %time%
for /L %%c in (65,1,83) DO (
   call :byteList2string myChar %%c
   call :dec2hex hexVal %%c 2
   echo %%c [decimal] = !hexVal! [hex] = char '!myChar!'
)

set byteList=65 66 67
call :byteList2string myString %byteList%
echo The byteList "%byteList%" converts to the string '!myString!'
echo %time%
goto :eof

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:byteList2string <resultChar> <byte-1> [<byte-2> ...<byte-n>]
:: Converts a list of bytes to a string, using the debug.exe
setlocal EnableDelayedExpansion
set /a count=1
set "hexBytes="
for %%t IN (%*) do (
  set /a count+=1
  if !count! GTR 2 (
   call :dec2hex hexVal %%t 2
   set "hexBytes=!hexBytes!!hexVal! "
  )
)
PUSHD "%temp%"
set "hexBytes=!hexBytes!0d 0a"
(
   echo e100
   echo %hexBytes%
   echo rcx
   echo %count%
   echo w
   echo q
) > DScript.tmp
:: You can pipe it directly to debug.exe, but the cmd-window flashes a bit
rem | debug %temp%\char.tmp > nul 2>nul

:: this way is slower, but it looks better
start "debug" /wait /min cmd /k "(type dscript.tmp | debug char.tmp) & exit"
rem del "%temp%\DScript.tmp" > nul 2> nul
POPD
(
   endlocal
   for /f "tokens=* usebackq" %%c in ("%temp%\char.tmp") do (
      set %~1=%%c
      )
   rem del "%temp%\char.tmp" > nul 2> nul
   goto :eof
)


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:dec2hex <hexResult> <decimalValue> <minimumDigits>
setlocal EnableDelayedExpansion
set digits=0123456789ABCDEF
set "result="
set /a value=%~2
set /a minDigits=%~3+0

:dec2hex.loop
set /a nibble = value %% 16
set /a value /= 16
set /a minDigits -=1
set result=!digits:~%nibble%,1!%result%

if %value% GTR 0 goto :dec2hex.loop
if %minDigits% GTR 0 goto :dec2hex.loop
(
   endlocal
   set %~1=%result%
   goto :eof
)

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:NormalizeVariable <varName>
:: Normalize a variable, so that it has no " " around, (not the same as %~1)
:: example  set temp="C:\my temp\"
:: Normalize convert it to temp=C:\my temp
:: usefull if you want to use the temp variable like echo hello > "%temp%\test.tmp"
setlocal EnableDelayedExpansion
call :__NormalizeVariable !%1!
(
   ENDLOCAL
   set "%1=%varResult%"
   goto :eof
)

:__NormalizeVariable <NormalizeValues>
:: help function for NormalizeVariable
:: Do not use directlty
if "%~2" EQU "" (
  set "varResult=%~1"
) ELSE (
   call :__NormalizeVariable "%*"
)
goto :eof


hope it helps
jeb

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Convert decimal to hex, and bytes to chars

#2 Post by ghostmachine4 » 09 Apr 2010 23:26

good effort, but no thanks. I would rather steer away and use a programming language with a function for that.

Code: Select all

C:\test>python
ActivePython 2.6.4.8 (ActiveState Software Inc.) based on
Python 2.6.4 (r264:75706, Nov  3 2009, 13:23:17) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> hex(4079)
'0xfef'
>>> chr(65)
'A'
>>> chr(66)
'B'
>>> chr(67)
'C'
>>> map(chr,[65,66,67])
['A', 'B', 'C']

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Convert decimal to hex, and bytes to chars

#3 Post by !k » 10 Apr 2010 06:44

Code: Select all

@echo off
call :create "%temp%\sbs2.com"

rem hex2str
echo.|"%temp%\sbs2.com" 0 "$0d$0a" "$07" >out
echo.|"%temp%\sbs2.com" 0 "$0d$0a" "$07$07$07" >>out
exit /b

:create the assembler program, by Herbert Kleebauer
echo.Bj@jzh`0X-`/PPPPPPa(DE(DM(DO(Dh(Ls(Lu(LX(LeZRR]EEEUYRX2Dx=>"%~1"
echo.0DxFP,0Xx.t0P,=XtGsB4o@$?PIyU!WvX0GwUY Wv;ovBX2Gv0ExGIuht6>>"%~1"
echo.?@}IKuNWpe~Fpe?FNHlF?wGMECIQqo{Ox{T?kPv@jeoSeIlRFD@{AyEKj@>>"%~1"
echo.iqe~1NeAyR?mHAG~BGRgB{~H?o~TsdgCYqe?HR~upkpBG?~slJBCyA?@xA>>"%~1"
echo.LZp{xq`Cs?H[C_vHDyB?Hos@QslFA@wQ~~x}viH}`LYNBGyA?@xAB?sUq`>>"%~1"
echo.LRy@PwtCYQEuFK@A~BxPtDss@fFqjVmzD@qBEOEenU?`eHHeBCMs?FExep>>"%~1"
echo.LHsPBGyA?@xAunjzA}EKNs@CA?wQpQpKLBHv?s`WJ`LRCYyIWMJaejCksl>>"%~1"
echo.H[GyFGhHBwHZjjHeoFasuFUJeHeB?OsQH[xeHCPvqFj@oq@eNc?~}Nu??O>>"%~1"
echo.~oEwoAjBKs?Zp`LBzHQzyEFrAWAG{EFrAqAGYwHTECIQ{coKIsaCsf{Oe~>>"%~1"
echo.CK}Ayre~CNFA{rAyEKFACrA{EKGAjbA}eKGSjNMtQFtc{OAyDGFj?{FDGQ>>"%~1"
echo.KAjNVk_OCAx@e?f{o?CosI}1EGizhljJ~H1ZeG}JBA~rACBMDGjjDG@g0>>"%~1"
goto :eof
Last edited by !k on 10 Apr 2010 07:48, edited 1 time in total.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Convert decimal to hex, and bytes to chars

#4 Post by ghostmachine4 » 10 Apr 2010 06:59

wow, seriously?

Post Reply