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.
Decimal to Binary (qbasic/BCX)
' 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
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.