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 were I look seems to be giving me the wrong information here is my code. thank you to all that reply
@ echo off
echo Hexa Decimal Convertor 1.0
set OneOn = false
set TwoOn = false
set ThreeOn = false
set FourOn = false
set FiveOn = false
set SixOn = false
set SevenOn = false
set EightOn = false
set firstDig = 0
set secondDig = 0
set digits = 128 64 32 16 8 4 2 1 0
set Total = 0
set INPUT = 43
set /p INPUT=Enter a number (0-255):
if %INPUT% gtr 15 goto other
if %INPUT% == 1 echo IN HEXADECIMAL %INPUT% = %INPUT%
if %INPUT% == 2 echo IN HEXADECIMAL %INPUT% = %INPUT%
if %INPUT% == 3 echo IN HEXADECIMAL %INPUT% = %INPUT%
if %INPUT% == 4 echo IN HEXADECIMAL %INPUT% = %INPUT%
if %INPUT% == 5 echo IN HEXADECIMAL %INPUT% = %INPUT%
if %INPUT% == 6 echo IN HEXADECIMAL %INPUT% = %INPUT%
if %INPUT% == 7 echo IN HEXADECIMAL %INPUT% = %INPUT%
if %INPUT% == 8 echo IN HEXADECIMAL %INPUT% = %INPUT%
if %INPUT% == 9 echo IN HEXADECIMAL %INPUT% = %INPUT%
if %INPUT% == 10 echo IN HEXADECIMAL %INPUT% = A
if %INPUT% == 11 echo IN HEXADECIMAL %INPUT% = B
if %INPUT% == 12 echo IN HEXADECIMAL %INPUT% = C
if %INPUT% == 13 echo IN HEXADECIMAL %INPUT% = D
if %INPUT% == 14 echo IN HEXADECIMAL %INPUT% = E
if %INPUT% == 15 echo IN HEXADECIMAL %INPUT% = F
pause
:other
for /l %%G (0, 1, 8) do (
for /l %%F (0,1,8) do (
for /l %%D (0,1,8) do (
for /l %%S (0,1,8) do (
for /l %%A (0,1,8) do (
for /l %%L (0,1,8) do (
for /l %%K (0,1,8) do (
for /l %%J (0,1,8) do (
for /l %%H (0,1,8) do (
set TESTTOTAL = digit[%H%] + digit[%J%] + digit[%K%] + digit[%L%] + digit[%A%] + digit[%S%] + digit[%D%] + digit[%F%] + digit[%G%]
if %TESTTOTAL% == %INPUT% goto match
)
)
)
)
)
)
)
)
)
)
:match
if digit[%H%] == 1 OneOn = true
if digit[%H%] == 2 TwoOn = true
if digit[%H%] == 4 ThreeOn = true
if digit[%H%] == 8 FourOn = true
if digit[%H%] == 16 FiveOn = true
if digit[%H%] == 32 SixOn = true
if digit[%H%] == 64 SevenOn = true
if digit[%H%] == 128 EightOn = true
if digit[%J%] == 1 OneOn = true
if digit[%J%] == 2 TwoOn = true
if digit[%J%] == 4 ThreeOn = true
if digit[%J%] == 8 FourOn = true
if digit[%J%] == 16 FiveOn = true
if digit[%J%] == 32 SixOn = true
if digit[%J%] == 64 SevenOn = true
if digit[%J%] == 128 EightOn = true
if digit[%K%] == 1 OneOn = true
if digit[%K%] == 2 TwoOn = true
if digit[%K%] == 4 ThreeOn = true
if digit[%K%] == 8 FourOn = true
if digit[%K%] == 16 FiveOn = true
if digit[%K%] == 32 SixOn = true
if digit[%K%] == 64 SevenOn = true
if digit[%K%] == 128 EightOn = true
if digit[%L%] == 1 OneOn = true
if digit[%L%] == 2 TwoOn = true
if digit[%L%] == 4 ThreeOn = true
if digit[%L%] == 8 FourOn = true
if digit[%L%] == 16 FiveOn = true
if digit[%L%] == 32 SixOn = true
if digit[%L%] == 64 SevenOn = true
if digit[%L%] == 128 EightOn = true
if digit[%A%] == 1 OneOn = true
if digit[%A%] == 2 TwoOn = true
if digit[%A%] == 4 ThreeOn = true
if digit[%A%] == 8 FourOn = true
if digit[%A%] == 16 FiveOn = true
if digit[%A%] == 32 SixOn = true
if digit[%A%] == 64 SevenOn = true
if digit[%A%] == 128 EightOn = true
if digit[%S%] == 1 OneOn = true
if digit[%S%] == 2 TwoOn = true
if digit[%S%] == 4 ThreeOn = true
if digit[%S%] == 8 FourOn = true
if digit[%S%] == 16 FiveOn = true
if digit[%S%] == 32 SixOn = true
if digit[%S%] == 64 SevenOn = true
if digit[%S%] == 128 EightOn = true
if digit[%D%] == 1 OneOn = true
if digit[%D%] == 2 TwoOn = true
if digit[%D%] == 4 ThreeOn = true
if digit[%D%] == 8 FourOn = true
if digit[%D%] == 16 FiveOn = true
if digit[%D%] == 32 SixOn = true
if digit[%D%] == 64 SevenOn = true
if digit[%D%] == 128 EightOn = true
if digit[%F%] == 1 OneOn = true
if digit[%F%] == 2 TwoOn = true
if digit[%F%] == 4 ThreeOn = true
if digit[%F%] == 8 FourOn = true
if digit[%F%] == 16 FiveOn = true
if digit[%F%] == 32 SixOn = true
if digit[%F%] == 64 SevenOn = true
if digit[%F%] == 128 EightOn = true
if digit[%G%] == 1 OneOn = true
if digit[%G%] == 2 TwoOn = true
if digit[%G%] == 4 ThreeOn = true
if digit[%G%] == 8 FourOn = true
if digit[%G%] == 16 FiveOn = true
if digit[%G%] == 32 SixOn = true
if digit[%G%] == 64 SevenOn = true
if digit[%G%] == 128 EightOn = true
if OneOn == true & TwoOn == true & ThreeOn == true & FourOn == true secondDig = F
if OneOn == true & TwoOn == true & ThreeOn == true & FourOn == false secondDig = E
if OneOn == true & TwoOn == true & ThreeOn == false & FourOn == true secondDig = D
if OneOn == false & TwoOn == false & ThreeOn == true & FourOn == true secondDig = C
if OneOn == true & TwoOn == true & ThreeOn == false & FourOn == true secondDig = B
if OneOn == false & TwoOn == true & ThreeOn == false & FourOn == true secondDig = A
if OneOn == true & TwoOn == false & ThreeOn == false & FourOn == true secondDig = 9
if OneOn == false & TwoOn == false & ThreeOn == false & FourOn == true secondDig = 8
if OneOn == true & TwoOn == true & ThreeOn == true & FourOn == false secondDig = 7
if OneOn == false & TwoOn == true & ThreeOn == true & FourOn == false secondDig = 6
if OneOn == true & TwoOn == false & ThreeOn == true & FourOn == false secondDig = 5
if OneOn == false & TwoOn == false & ThreeOn == true & FourOn == false secondDig = 4
if OneOn == true & TwoOn == true & ThreeOn == false & FourOn == false secondDig = 3
if OneOn == false & TwoOn == true & ThreeOn == false & FourOn == false secondDig = 2
if OneOn == true & TwoOn == false & ThreeOn == false & FourOn == false secondDig = 1
if OneOn == false & TwoOn == false & ThreeOn == false & FourOn == false secondDig = 0
if FiveOn == true & SixOn == true & SevenOn == true & EightOn == true firstDig = F
if FiveOn == true & SixOn == true & SevenOn == true & EightOn == false firstDig = E
if FiveOn == true & SixOn == true & SevenOn == false & EightOn == true firstDig = D
if FiveOn == false & SixOn == false & SevenOn == true & EightOn == true firstDig = C
if FiveOn == true & SixOn == true & SevenOn == false & EightOn == true firstDig = B
if FiveOn == false & SixOn == true & SevenOn == false & EightOn == true firstDig = A
if FiveOn == true & SixOn == false & SevenOn == false & EightOn == true firstDig = 9
if FiveOn == false & SixOn == false & SevenOn == false & EightOn == true firstDig = 8
if FiveOn == true & SixOn == true & SevenOn == true & EightOn == false firstDig = 7
if FiveOn == false & SixOn == true & SevenOn == true & EightOn == false firstDig = 6
if FiveOn == true & SixOn == false & SevenOn == true & EightOn == false firstDig = 5
if FiveOn == false & SixOn == false & SevenOn == true & EightOn == false firstDig = 4
if FiveOn == true & SixOn == true & SevenOn == false & EightOn == false firstDig = 3
if FiveOn == false & SixOn == true & SevenOn == false & EightOn == false firstDig = 2
if FiveOn == true & SixOn == false & SevenOn == false & EightOn == false firstDig = 1
if FiveOn == false & SixOn == false & SevenOn == false & EightOn == false firstDig = 0
echo in hexadecimal %INPUT% = %firstDig% %secondDig%
pause
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With