' Conversion from Base 2 to Base 10 ' Written by Michael L. Jones, M. S., Spring 2002 ' Suffolk County Community College, Brentwood, New York ' Plan of attack: Input a number in base 2. Check that the number is in ' fact a binary number. The input number will be entered as a string to ' allow for large numbers and for portability to other bases. Start with ' the first bit, multiply by 2, add the next bit, multiply by 2 again, and so ' on until the last bit is added. Output the result. CLS INPUT "What is the binary number"; bin$ PRINT DEFLNG D 'Allows for large numbers dec = 0 'Initalize decimal number l = LEN(bin$) 'Get number of digits in binary number FOR i = 1 TO l n = -1 'Sentinel value 'Set ith digit to its decimal value IF MID$(bin$, i, 1) = "0" THEN n = 0 IF MID$(bin$, i, 1) = "1" THEN n = 1 'If the character is not a binary digit, n remains -1 IF n = -1 THEN GOTO fail dec = 2 * dec + n 'Multiply by 2 and add new digit value NEXT i PRINT "The decimal equivalent of "; bin$; " is"; dec; "." END fail: PRINT bin$; " is not a binary number."