-
qbasic (
http://www.daniweb.com/code/qbasic.html)
| vegaseat | qbasic syntax Jan 16th, 2005 | |
| One more look at BCX basic code to show the qbasic programmers how they can modernize. This shows a function that converts an integer value to a binary string. Notice that with BCX basic you need to declare your variables. Makes the code a little easier to read too. Variable, Function and Sub names are case sensitive, keywords are not. |
' converting a decimal integer to a binary string
' BCX basic
'
'
' postfix $ indicates a string variable
' could be written as: dim A as String
'
DIM A$
A$ = dec2bin$(123)
PRINT "decimal 123 = binary ", A$
A$ = dec2bin$(255)
PRINT "decimal 255 = binary ", A$
pause ' wait
'
' postfix $ in function name indicates that the function returns a string
' could be written as: function dec2bin Optional (a, digits=0) as String
' keyword Optional indicates parameter digits is optional and defaults to 0
' variables declared within the function are local
'
FUNCTION dec2bin$ Optional (a, digits=0)
DIM k, tmp, t ' default to integer variables
DIM T$ ' string variable
' don't know the number of digits in a, so figure it out
IF digits = 0 THEN
tmp = a
WHILE tmp > 0
tmp = tmp >> 1 ' shift right by 1 place
digits++ ' increment by 1, same as: digits = digits + 1
WEND
END IF
t = 1
' do the conversion to zeros and ones
FOR k = 1 TO digits
tmp = a AND t
IF tmp = 0 THEN
T$ = "0" & T$
ELSE
T$ = "1" & T$
END IF
t = t << 1 ' shift left by 1 place
NEXT
FUNCTION = T$ ' result is in T$, return this
END FUNCTION