0

```
' 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
```

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.