Page 1 of 1

Correcting Decimal to Binary and Hexadecimal converter

Posted: 22 Jan 2013 03:30
by john924xps
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

Re: Correcting Decimal to Binary and Hexadecimal converter

Posted: 22 Jan 2013 04:16
by foxidrive
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
)

Re: Correcting Decimal to Binary and Hexadecimal converter

Posted: 22 Jan 2013 05:05
by john924xps
Uh... what do you want me to do with those snips of code? They both act the same way

Re: Correcting Decimal to Binary and Hexadecimal converter

Posted: 22 Jan 2013 05:13
by john924xps
Whoa. Somehow, :: and rem are actually a bit different. I swapped my ::s with "rem" and now there are no parenthesis errors! THANKS

Re: Correcting Decimal to Binary and Hexadecimal converter

Posted: 22 Jan 2013 05:19
by foxidrive
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.

Re: Correcting Decimal to Binary and Hexadecimal converter

Posted: 22 Jan 2013 18:14
by john924xps
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

Re: Correcting Decimal to Binary and Hexadecimal converter

Posted: 22 Jan 2013 18:32
by abc0502
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.

Re: Correcting Decimal to Binary and Hexadecimal converter

Posted: 22 Jan 2013 19:00
by Aacini
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

Re: Correcting Decimal to Binary and Hexadecimal converter

Posted: 23 Jan 2013 07:32
by john924xps
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

Re: Correcting Decimal to Binary and Hexadecimal converter

Posted: 23 Jan 2013 08:02
by Aacini
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