Nov 9, 2013

Function: Dec2Hex

The quick way which makes use of a feature that you might not necessarily have known, which is in the REG command: when writing a DWORD/QWORD value and giving data in decimal, it converts the input to hexadecimal for you.
If the OS is 32-bit, over-32-bit-values are not supported. When 32-bit, if it's given with a value that is greater than or equal to 4294967295 (232-1), then it returns 0xFFFFFFFF.
If the OS is 64-bit, and if the input is greater than 18446744073709551615 (264-1), the function fails.
@echo off
:loop
echo.
set/p input=give a decimal number 
call:Dec2Hex input output
if %errorlevel%==0 (echo  %input% --^> %output%)
if %errorlevel%==1 (echo  Could not carry out conversion.)
goto loop
:Dec2Hex
setlocal
set v0=%% %1 %%
set v0=%v0: =%
call set num=%v0%
set err=0
set arch=64
set v0=%processor_architecture%
set v0=%v0:86=%
if not "%v0%"=="%processor_architecture%" (set/a arch/=2)
if "%arch%"=="32" (set len=D) else (set len=Q)
reg add "HKCU" /v "dec2hex_removeme" /t "REG_%len%WORD" /d "%num%" /f>nul 2>&1||set err=1
if not "%err%"=="1" (for /f "tokens=3" %%A in ('reg query "HKCU" /v "dec2hex_removeme"') do (set hex=%%A))
if defined hex (reg delete "HKCU" /v "dec2hex_removeme" /f>nul 2>&1)
endlocal&set %2=%hex%&exit/b%err%

No comments:

Post a Comment