okay so I have been working an a batch file that converts a base ten decimal value in to hexadecimal but I can\'t get the for loops and the if statements down right every we
Supposing you want to convert hexadecimal to decimal numbers:
Why not simply using set /A
to do the job on its own? set /A
is capable of converting hexadecimal to decimal numbers when prefixed with 0x
. See set /?
for details.
The line below stores the decimal number in DEC
, supposing HEX
contains a hexadecimal number:
set /A DEC=0x%HEX%
The following script prompts the user for a hex. number and displays its decimal representation:
@echo off
:LOOP
set "HEX="
set /P "HEX=Enter a hexadecimal number (up to 8 digits): "
if not defined HEX goto :EOF
set /A DEC=0x%HEX%
echo The decimal representation of %HEX% is %DEC%.
goto :LOOP
Note that the hexadecimal number is limited to 8 digits (32 bits), and it is interpreted as signed value.
Supposing you want to convert decimal to hexadecimal numbers:
There is an undocumented built-in environment variable called =ExitCode
that holds the hexadecimal character code (ASCII) of the current return or exit code (usually the ErrorLevel
).
The lines below store the hexadecimal number in HEX
, supposing DEC
contains a decimal number:
cmd /C exit %DEC%
set "HEX=%=ExitCode%"
The following script prompts the user for a decimal number and displays its hex. representation:
@echo off
:LOOP
set "DEC="
set /P "DEC=Enter a decimal number (signed 32-bit integer): "
if not defined DEC goto :EOF
cmd /C exit %DEC%
set "HEX=%=ExitCode%"
for /F "tokens=* delims=0" %%Z in ("%HEX%") do set "HEX=%%Z"
if not defined HEX set "HEX=0"
echo The hexadecimal representation of %DEC% is %HEX%.
goto :LOOP