Binary To Decimal

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
jimmy
Newbie
Posts: 3
Joined: Fri Jun 05, 2009 8:00 am

Binary To Decimal

Post by jimmy »

Hi
How do you convert 7bit binary to decimal in QBASIC?
Thanks in advance.
Harry Potter
Veteran
Posts: 111
Joined: Sat Feb 21, 2009 8:19 am
Location: New York, U.S.

Post 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.
Joseph Rose, a.k.a. Harry Potter
Creating magic in the computer community...or at least striving to! :(
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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
Last edited by burger2227 on Fri Jun 05, 2009 8:47 pm, edited 1 time in total.
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

nice piece of code ted. works as expected :)
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
jimmy
Newbie
Posts: 3
Joined: Fri Jun 05, 2009 8:00 am

Post by jimmy »

Thanks heaps. Works really well.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
jimmy
Newbie
Posts: 3
Joined: Fri Jun 05, 2009 8:00 am

Post by jimmy »

Also, how would you split binary up into 7 to convert it?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

What do you mean? Please explain...........convert it to what?
Last edited by burger2227 on Sat Jun 06, 2009 10:14 pm, edited 1 time in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

I think he wants to know more details about how the second loop in your code work. :)
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
kidingwithlaura
Newbie
Posts: 1
Joined: Tue Mar 01, 2016 10:41 am

Re: Binary To Decimal

Post 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
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: Binary To Decimal

Post by burger2227 »

Come back if you need more help.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply