Convert decimal to hex, and bytes to chars
Posted: 09 Apr 2010 11:56
Hi,
for those who are interested.
Three functions (perhaps anyone find it usefull).
:dec2hex
Converts a number to hex
Convert one or more bytes into a string
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
The last function is a bit complicated, particulary the intention.
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.
hope it helps
jeb
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.
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