Posted: 6th Jul 2021 6:46
Here's a piece of code I knocked up in couple of mins which converts decimal whole numbers to either binary, hex, octal. of course we have functions hex$ and bin$ but wanted to test out octal (base 8) also.
I wrote a recursive function which worked to a point, but "endfunction res$" for example did not return the final string, and am working on a differernt way to get it to return correctly.
No code to handle negative numbers yet i.e. the complement system

+ Code Snippet
Rem Project: value2base_v0.1
Rem Created: Tuesday, July 06, 2021

Rem ***** Main Source File *****

sync on : sync 30 : sync

decval=255
base=2

binary$=value2base(decval,2)
octal$=value2base(decval,base+6)
hexdec$=value2base(decval,base+14)
print str$(decval);" -> ";binary$;" (binary)"
print str$(decval);" -> ";octal$;" (octal)"
print str$(decval);" -> ";hexdec$;" (hexadecimal)"

sync
wait key

end

function value2base(v,base)
	local b$, t$, rmd, v2
	repeat
		rmd = v mod base
		v2 = v / base
		`print v; ", ";v2; ", ";rmd;", base= ";base // debug only
		v = v2
		if base = 16
			if rmd >=10 and rmd<=15
				if rmd=10 then t$="A"
				if rmd=11 then t$="B"
				if rmd=12 then t$="C"
				if rmd=13 then t$="D"
				if rmd=14 then t$="E"
				if rmd=15 then t$="F"
			else
				t$=str$(rmd)
			endif
		else
			t$=str$(rmd)
		endif
		b$=b$+t$
	until v<=0
		
	for l=len(b$) to 1 step -1
		res$=res$+mid$(b$,l)
	next i
endfunction res$

Posted: 6th Jul 2021 7:51
recursion version:

+ Code Snippet
sync on : sync 30 : sync

decval=13
base=2

binary$=value2baser(decval,2)
octal$=value2baser(decval,base+6)
hexdec$=value2baser(decval,base+14)

`binary$=value2base(decval,2)
`octal$=value2base(decval,base+6)
`hexdec$=value2base(decval,base+14)
print str$(decval);" -> ";binary$;" (binary)"
print str$(decval);" -> ";octal$;" (octal)"
print str$(decval);" -> ";hexdec$;" (hexadecimal)"

sync
wait key

end

function value2base(v,base)
	local b$, t$, rmd, v2
	repeat
		rmd = v mod base
		v2 = v / base
		`print v; ", ";v2; ", ";rmd;", base= ";base // debug only
		v = v2
		if base = 16
			if rmd >=10 and rmd<=15
				if rmd=10 then t$="A"
				if rmd=11 then t$="B"
				if rmd=12 then t$="C"
				if rmd=13 then t$="D"
				if rmd=14 then t$="E"
				if rmd=15 then t$="F"
			else
				t$=str$(rmd)
			endif
		else
			t$=str$(rmd)
		endif
		b$=b$+t$
	until v<=0
		
	for l=len(b$) to 1 step -1
		res$=res$+mid$(b$,l)
	next i
endfunction res$

function value2baser(v,base)
	a$=value2basermain$(v,base,"")		
endfunction a$

function value2basermain$(v,base,res$)
	if v > 0
		rmd = v mod base
		v2 = v / base
		`print v; ", ";v2; ", ";rmd
		v = v2
		if base = 16
			if rmd >=10 and rmd<=15
				if rmd=10 then t$="A"
				if rmd=11 then t$="B"
				if rmd=12 then t$="C"
				if rmd=13 then t$="D"
				if rmd=14 then t$="E"
				if rmd=15 then t$="F"
			else
				t$=str$(rmd)
			endif
		else
			t$=str$(rmd)
		endif

		res$=res$+t$
		`print t$;", ";res$
		res$=value2basermain$(v,base,res$)
	endif
	for l=len(res$) to 1 step -1
		result$=result$+mid$(res$,l)
	next i
endfunction result$ 

Posted: 7th Jul 2021 12:12
2's complement to be added to code. This is for those not familiar with 2's complemente, is where all the bits get reversed and +1 added. range for neg values would be -128 -> +127 e.g. -128 would be stored as 10000000
Posted: 9th Jul 2021 7:14
of course doing it the other way around i.e. binary to decimal:

+ Code Snippet
Rem ***** Main Source File *****
sync on : sync rate 30: sync
// 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 //
`bin2dec = (2^0)+(2^1)+(2^2)+(2^3)+(2^4)+(2^5)+(2^6)+(2^7)
dim binary$(15)
b$="0000000000000111"
for i=len(b$) to 1 step -1
	binary$(len(b$)-i)=mid$(b$,i)
	bn$=bn$+mid$(b$,i,1)
next i
decval=0

for b=0 to 15
	if binary$(b)="1"
		inc decval,2^b
	endif    	
next b

print
print "decimal: ";decval

print %1111111111111001
print %1111111111111111

a= (%1111111111111001)
b= (%1111111111111111)+1

print a-b
print bin$(-7)
Sync

wait key
end


To get -7, we use 2's complement (and adding 1).. so 65529-65535(+1)= -7

2's complement for those that don't know, is where the bits get reversed e.g. 7 in binary = 00000111 and 2's complement 11111001