2

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
aschipfl
  • 31,767
  • 12
  • 51
  • 89
user1610489
  • 41
  • 1
  • 2
  • 3
  • The second search result I found in Google for `convert decimal to hex batch file` was [this one](http://blogs.msdn.com/b/joshpoley/archive/2011/06/29/hex-conversion-via-a-batch-file.aspx), which seems to work just fine. – Ken White Oct 08 '15 at 01:40
  • @KenWhite but here is supposed to be `hexadecimal` to `decimal` – Paul Oct 08 '15 at 01:43
  • Code in the link I provided does both (hex to decimal or decimal to hex). Searching for `convert hex to decimal batch file` turns up the same exact link I posted. Did you *read it*? – Ken White Oct 08 '15 at 01:48
  • @KenWhite Google does not always tell the truth. It was not because he return you the same link when you mix the words that means be relevant. And yes I have tested the link. For example it convert 255 to 0xFF but not the opposite. – Paul Oct 08 '15 at 02:02
  • OK. Sorry for trying to help you learn how to search Google. Whether it turned up the same result as second or not is irrelevant - what matters is **it turned up the same result**. The **code is relevant** for doing both conversions, as I said before. – Ken White Oct 08 '15 at 02:11
  • @KenWhite Have you at least tried the code from the link you provided and are we talking about the same link? You are mistaken – Paul Oct 08 '15 at 02:24
  • 1
    See: http://stackoverflow.com/questions/32961479/format-a-hexadecimal-sequence-in-a-cmd-exe-batch-file/32967341#32967341 – Aacini Oct 08 '15 at 02:51
  • There is also http://www.dostips.com/DtTipsArithmetic.php#toDec – Paul Oct 08 '15 at 02:53
  • 2
    @user1610489 Please edit the question to make clear what you really want to do. The title is about __hexadecimal to decimal__ but in first sentence in body you wrote __decimal to hexadecimal__. So what is your batch file really for? Do you know that every Windows has `calc`, a calculator which when set to Programmer (Windows Vista or later) or Scientific (Windows XP and before) can convert decimal numbers to hexadecimal (key F5) and vice versa (key F6) and also octal (F7) and binary (F8)? Sysinternals [Hex2dec](https://technet.microsoft.com/en-us/sysinternals/bb896736) can be used on cmd line. – Mofi Oct 08 '15 at 05:20

3 Answers3

10

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
aschipfl
  • 31,767
  • 12
  • 51
  • 89
4

Phew - what a lot of Code. This is a little bit shorter:

@echo off
setlocal enabledelayedexpansion
set "hex=0123456789ABCDEF"
set /p INPUT=Enter a number (0-255):
set /a high=%INPUT% / 16
set /a low=%INPUT% %% 16
echo in hexadecimal %INPUT% = !hex:~%high%,1! !hex:~%low%,1!
Stephan
  • 50,835
  • 10
  • 55
  • 88
  • 1
    You may use just one SET /A command: `set /a high=INPUT / 16, low=INPUT %% 16` **`;)`** – Aacini Oct 08 '15 at 13:42
  • @Aacini yes, and save a few spaces, but I like to keep things readable (this is not tagged [code golf](http://stackoverflow.com/questions/tagged/code-golf) `;)`) – Stephan Oct 08 '15 at 19:55
2

Save this with .bat extension:

@if (@x)==(@y) @end /***** jscript comment ******
     @echo off

     cscript //E:JScript //nologo "%~f0" %*
     exit /b 0

 @if (@x)==(@y) @end ******  end comment *********/

 var args=WScript.Arguments;
 var number=args.Item(0);

 WScript.Echo(parseInt(number,16));

And use it like

for /f %%# in ('hexToDec.bat 10E4') do set "decimalNumber=%%#"
echo %decimalNumber%
npocmaka
  • 53,069
  • 18
  • 138
  • 177
  • 3
    The same in one line! **`:-)`** `@set @a=0 /* & CScript //E:JScript //nologo "%~F0" %* & exit /b 0 */ WScript.Echo(parseInt(WScript.Arguments(0),16));` – Aacini Oct 08 '15 at 13:57