Bug with strings?

Previous topic - Next topic

PeeJay

I have written a *very* rough routine for converting a number into 8 bit binary format -

Code (glbasic) Select
test$=Convert8toBin(1)
PRINT test$,200,200

SHOWSCREEN ; KEYWAIT

FUNCTION Convert8toBin: num

LOCAL n$

IF num>127
n$="1"
num=num-128
ELSE
n$="0"
ENDIF

IF num>63
n$=n$+"1"
num=num-64
ELSE
n$=n$+"0"
ENDIF

IF num>31
n$=n$+"1"
num=num-32
ELSE
n$=n$+"0"
ENDIF

IF num>15
n$=n$+"1"
num=num-16
ELSE
n$=n$+"0"
ENDIF

IF num>7
n$=n$+"1"
num=num-8
ELSE
n$=n$+"0"
ENDIF

IF num>3
n$=n$+"1"
num=num-4
ELSE
n$=n$+"0"
ENDIF

IF num>1
n$=n$+"1"
num=num-2
ELSE
n$=n$+"0"
ENDIF

IF num>0
n$=n$+"1"
num=num-1
ELSE
n$=n$+"0"
ENDIF

RETURN n$

ENDFUNCTION
The code is not elegant, but it should work. The bug is this - I expect when I run it for it to return "00000001", but instead, all of the leading zeroes are missing. Since it is a string variable, the "0"'s should be there.
www.peejays-remakes.co.uk
For games, remakes, and GL Basic Tutorials
Artificial Intelligence is no match for Natural Stupidity

AndyH

Try declaring your function to return a string:

FUNCTION Convert8toBin$: num

PeeJay

Ah, that works - thanks Andy :)
www.peejays-remakes.co.uk
For games, remakes, and GL Basic Tutorials
Artificial Intelligence is no match for Natural Stupidity