Page 1 of 1

Binary To Decimal

Posted: Fri Jun 05, 2009 8:03 am
by jimmy
Hi
How do you convert 7bit binary to decimal in QBASIC?
Thanks in advance.

Posted: Fri Jun 05, 2009 11:42 am
by Harry Potter
Follow these steps:

1. Set the output number to 0 and variable A to 1.
2. Get the right-most digit of input.
3. If it is 1, add A to the output.
4. Multiply A by 2 and shift right input one bit.
5. If input<>0, go to step 2.
6. Return output.

Posted: Fri Jun 05, 2009 12:45 pm
by burger2227
The following routine will work for up to 15 binary places:

Code: Select all

SUB Bin2Dec
COLOR 14: LOCATE 2, 30: PRINT "Binary to Decimal"
COLOR 10: LOCATE 5, 20: PRINT "Enter a binary number (1's or 0's):"

DO: Bin$ = INKEY$ 'string number entry 1 or 0 with backspace
  IF Bin$ = "1" OR Bin$ = "0" THEN Binary$ = Binary$ + Bin$
  L = LEN(Binary$)
  IF L > 0 AND Bin$ = CHR$(8) THEN Binary$ = MID$(Binary$, 1, L - 1)
  COLOR 14: LOCATE 5, 57: PRINT Binary$; " "
LOOP UNTIL L > 0 AND Bin$ = CHR$(13) 'enter quits 

FOR i = 0 TO L - 1
     place = VAL(MID$(Binary$, L - i, 1))
     dec = place * 2 ^ i
     Decimal = Decimal + dec
NEXT
COLOR 11: LOCATE 10, 30: PRINT "Decimal number ="; Decimal

END SUB

Posted: Fri Jun 05, 2009 8:03 pm
by MystikShadows
nice piece of code ted. works as expected :)

Posted: Fri Jun 05, 2009 8:48 pm
by jimmy
Thanks heaps. Works really well.

Posted: Fri Jun 05, 2009 11:11 pm
by burger2227
You're welcome. I like to use INKEY$ loops better than INPUT. You can filter out bad entries better.

Ted

PS: Thanks Myst.

Posted: Sat Jun 06, 2009 12:35 am
by jimmy
Also, how would you split binary up into 7 to convert it?

Posted: Sat Jun 06, 2009 4:54 pm
by burger2227
What do you mean? Please explain...........convert it to what?

Posted: Sat Jun 06, 2009 6:20 pm
by MystikShadows
I think he wants to know more details about how the second loop in your code work. :)

Re: Binary To Decimal

Posted: Tue Mar 01, 2016 10:46 am
by kidingwithlaura
Hello,

I am not a programmer. I was just searching for a binary to decimal converter tool and landed here. Although here is not tool and I found one resource as well but just want to thank you all coders for making our lives easy by creating such useful tools.

Laura

Re: Binary To Decimal

Posted: Tue Mar 01, 2016 4:08 pm
by burger2227
Come back if you need more help.