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

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

Binary To Decimal

Postby jimmy » Fri Jun 05, 2009 8:03 am

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.

Postby Harry Potter » Fri Jun 05, 2009 11:42 am

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: 2144
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Postby burger2227 » Fri Jun 05, 2009 12:45 pm

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:

Postby MystikShadows » Fri Jun 05, 2009 8:03 pm

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

Postby jimmy » Fri Jun 05, 2009 8:48 pm

Thanks heaps. Works really well.

User avatar
burger2227
Veteran
Posts: 2144
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Postby burger2227 » Fri Jun 05, 2009 11:11 pm

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 : http://www.QB64.NET
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

Postby jimmy » Sat Jun 06, 2009 12:35 am

Also, how would you split binary up into 7 to convert it?

User avatar
burger2227
Veteran
Posts: 2144
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Postby burger2227 » Sat Jun 06, 2009 4:54 pm

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 : http://www.QB64.NET
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:

Postby MystikShadows » Sat Jun 06, 2009 6:20 pm

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

Postby kidingwithlaura » Tue Mar 01, 2016 10:46 am

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: 2144
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: Binary To Decimal

Postby burger2227 » Tue Mar 01, 2016 4:08 pm

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 : http://www.QB64.NET
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0


Return to “QBASIC and QB64 Questions & Answers”

Who is online

Users browsing this forum: Bing [Bot] and 3 guests

cron