Convert to Binary in BBC SDL ?

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
User avatar
zachnoland
Posts: 17
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

Convert to Binary in BBC SDL ?

Post by zachnoland »

I know that in BBC BASIC you can declare or initialize integer variables by filling them with binary with % at the beginning of the binary number. Like this:

Code: Select all

10 MYBIN% = %001001
20 PRINT MYBIN%
So the output is the result of the binary number in the form of an integer.
And I've read some documentation on BBC SDL it seems, there is FN_binary(B%) but it requires additional libraries.
So the question is, is there a way to convert Integer or other variables to binary?
jgharston
Posts: 39
Joined: Thu 05 Apr 2018, 14:08

Re: Convert to Binary in BBC SDL ?

Post by jgharston »

290 REM Binary padded with zeros
300 DEFFNb0(A%,N%):LOCAL A$,B$,L%:B$="0":IFA%<0:B$="1":A%=A%AND&7FFFFFFF
310 REPEATA$=STR$(A%AND1)+A$:A%=A%DIV2:L%=L%+1:UNTILL%>30:=RIGHT$(B$+A$,N%)
320 :
330 REM Binary padded with spaces
340 DEFFNb(A%,N%):LOCAL A$:IFA%<0:=FNb0(A%,N%)
350 REPEATA$=STR$(A%AND1)+A$:A%=A%DIV2:UNTILA%=0:=RIGHT$(STRING$(N%," ")+A$,N%)

mdfs.net/blib

PRINT FNb0(number, number_of_digits)
Richard Russell
Posts: 366
Joined: Tue 18 Jun 2024, 09:32

Re: Convert to Binary in BBC SDL ?

Post by Richard Russell »

zachnoland wrote: Sun 20 Apr 2025, 02:01 So the question is, is there a way to convert Integer or other variables to binary?
If you simply want to avoid the use of a library, I would suggest you copy FN_tobase() from stringlib.bbc into your program. This will convert to any base, not just binary.
User avatar
zachnoland
Posts: 17
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

Re: Convert to Binary in BBC SDL ?

Post by zachnoland »

jgharston wrote: Sun 20 Apr 2025, 08:18 290 REM Binary padded with zeros
300 DEFFNb0(A%,N%):LOCAL A$,B$,L%:B$="0":IFA%<0:B$="1":A%=A%AND&7FFFFFFF
310 REPEATA$=STR$(A%AND1)+A$:A%=A%DIV2:L%=L%+1:UNTILL%>30:=RIGHT$(B$+A$,N%)
320 :
330 REM Binary padded with spaces
340 DEFFNb(A%,N%):LOCAL A$:IFA%<0:=FNb0(A%,N%)
350 REPEATA$=STR$(A%AND1)+A$:A%=A%DIV2:UNTILA%=0:=RIGHT$(STRING$(N%," ")+A$,N%)

mdfs.net/blib

PRINT FNb0(number, number_of_digits)
Great code :)
I tried to sort the code:

Code: Select all

10 REM TRY CONVERT INTEGER TO BINARY 
20 PRINT FNb0(8, 8)
30 END
40
290 REM Binary padded with zeros
300 DEF FNb0(A%,N%):LOCAL A$,B$,L%:B$="0":IFA%<0:B$="1":A%=A%AND&7FFFFFFF
310 REPEATA$=STR$(A%AND1)+A$:A%=A%DIV2:L%=L%+1:UNTILL%>30:=RIGHT$(B$+A$,N%)
320 
330 REM Binary padded with spaces
340 DEF FNb(A%,N%):LOCAL A$:IFA%<0:=FNb0(A%,N%)
350 REPEATA$=STR$(A%AND1)+A$:A%=A%DIV2:UNTILA%=0:=RIGHT$(STRING$(N%," ")+A$,N%)
Richard Russell wrote: Sun 20 Apr 2025, 09:21 If you simply want to avoid the use of a library, I would suggest you copy FN_tobase() from stringlib.bbc into your program. This will convert to any base, not just binary.
Yeah, that could be an option. I just found out about the library lol it's stringlib.bbc, Maybe this will be the first library in BBC SDL I use :)

Thank You :D