Correcting Decimal to Binary and Hexadecimal converter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Correcting Decimal to Binary and Hexadecimal converter

#1 Post by john924xps » 22 Jan 2013 03:30

Hey guys!
I created a program that converts decimal to hexadecimal and binary, but I have a whole bunch of glitches.
One of the *current* glitches are "( was not expected", but when I looked through the code I couldn't find any unnecessary parenthesis.
Another says that "%chars:~!tempnumHEX!,1%%finalnumHEX%" isn't computable. It's supposed to get the modulus of an equation and use the variable chars to find the corresponding value of hexadecimal for it.

Though if I ignore the errors and type in "5" for the program to convert, it displays nothing for the hex and 01 for the binary. So I'm assuming the error takes place in the hexadecimal conversion part?

Thanks.

Code: Select all

@echo off
title Decimal to Binary ^& Hexadecimal
color 0b
setlocal EnableDelayedExpansion
set chars= 123456789ABCDEF

:ask
cls
set /p convertnumHEX=Number to convert:
set convertnumBIN=%convertnumHEX%

:main
::checks if it is done converting to hexadecimal
if %convertnumHEX%==0 (
::if done with hex, checks for binary
if %convertnumBIN%==0 (
::if done with binary, then goes to display results
goto :results
) else (
::otherwise, if it isnt done converting to binary
::it converts it
set /a modulusBIN=convertnumBIN %% 2
set /a convertnumBIN=convertnumBIN / 2
set finalnumBIN=%modulusBIN%%finalnumBIN%
::done with producing a digit of the binary
)) else (
::now this is when it isn't done converting to hexadecimal
set /a modulusHEX=convertnumHEX %% 16
set /a convertnumHEX=%convertnumHEX% / 16
set finalnumHEX=%chars:~!modulusHEX!,1%%finalnumHEX%
)
::goes back to main to check for any more needed conversions
goto :main

:results
echo hex %finalnumHEX%
echo bin %finalnumBIN%
pause
Last edited by john924xps on 22 Jan 2013 05:32, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Correcting Decimal to Binary and Hexadecimal converter

#2 Post by foxidrive » 22 Jan 2013 04:16

Try the first code below, and then the second.


Code: Select all

(
echo abc
::done with producing a digit of the binary
)



(
echo abc
rem done with producing a digit of the binary
)

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Correcting Decimal to Binary and Hexadecimal converter

#3 Post by john924xps » 22 Jan 2013 05:05

Uh... what do you want me to do with those snips of code? They both act the same way

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Correcting Decimal to Binary and Hexadecimal converter

#4 Post by john924xps » 22 Jan 2013 05:13

Whoa. Somehow, :: and rem are actually a bit different. I swapped my ::s with "rem" and now there are no parenthesis errors! THANKS

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Correcting Decimal to Binary and Hexadecimal converter

#5 Post by foxidrive » 22 Jan 2013 05:19

john924xps wrote:Uh... what do you want me to do with those snips of code? They both act the same way



No, they don't. It was discussed here in a thread recently.

:: on the line directly before a
)

causes the error.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Correcting Decimal to Binary and Hexadecimal converter

#6 Post by john924xps » 22 Jan 2013 18:14

Now this line won't compute correctly.
set finalnumHEX=%chars:~!modulusHEX!,1%%finalnumHEX%
When I type in 5 for the number to convert, this is the value of finalnumHEX.
chars:~5,1%finalnumHEX
That's it. It just won't compute

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Correcting Decimal to Binary and Hexadecimal converter

#7 Post by abc0502 » 22 Jan 2013 18:32

Try using separate If statements instead of IF/ELSE, sometimes the If/Else statement always take one condition "usually the first" and ignore the other conditions, i noticed that while coding before.

Aacini
Expert
Posts: 1887
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Correcting Decimal to Binary and Hexadecimal converter

#8 Post by Aacini » 22 Jan 2013 19:00

john924xps wrote:Now this line won't compute correctly.
set finalnumHEX=%chars:~!modulusHEX!,1%%finalnumHEX%
When I type in 5 for the number to convert, this is the value of finalnumHEX.
chars:~5,1%finalnumHEX
That's it. It just won't compute


Remember that DELAYED expansion, that is, a variable enclosed in exclamation marks instead of percent signs, happen after the "normal" percent expansion (hence the name). This mean that in %chars:~!modulusHEX!,1% the whole expression expand first, when !modulusHEX! has NOT been expanded! To solve your problem, you need to exchange normal and delayed expansion this way:

Code: Select all

set finalnumHEX=!chars:~%modulusHEX%,1!!finalnumHEX!
That is, expand first the modulusHEX value, then get that character from chars.

Antonio

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Correcting Decimal to Binary and Hexadecimal converter

#9 Post by john924xps » 23 Jan 2013 07:32

Thanks Aacini. That worked and the program's now operable. But everytime you enter a large number, it says that the computer is limited to 32 bits of precision. How can I bypass this?

THANKS AGAIN

Aacini
Expert
Posts: 1887
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Correcting Decimal to Binary and Hexadecimal converter

#10 Post by Aacini » 23 Jan 2013 08:02

You may divide the large number in smaller chunks of digits, so each part can be managed in 32-bits integers. For Dec to Hex conversion you must use groups of 4 digits, but for Dec to Binary you may use groups of up to 9 decimal digits.

Full details here: viewtopic.php?f=3&t=4022&p=22370#p22370

Antonio

Post Reply