
Converting a decimal number stored as a string into Binary, Octal and Hexadecimal
In this tutorial we are going to manually convert a decimal number stored inside a string into:
• Base 2 (Binary)
• Base 8 (Octal)
• Base 16 (Hexadecimal)
This example avoids built-in conversion commands on purpose, so beginners can see how the process works internally.
Example Output Usage
s$="87654321" print s$ +"="+ ConvertTo(S$,2) print s$ +"="+ ConvertTo(S$,8) print s$ +"="+ ConvertTo(S$,16) print "" s$="-12345678" print s$ +"="+ ConvertTo(S$,2) print s$ +"="+ ConvertTo(S$,8) print s$ +"="+ ConvertTo(S$,16) print "" s$="255" print s$ +"="+ ConvertTo(S$,2) print s$ +"="+ ConvertTo(S$,8) print s$ +"="+ ConvertTo(S$,16) print "" Sync waitkey
Step 1: Manually Converting the String to an Integer
Before we can convert to another base, we must first turn the string into an actual integer value.
This is done digit-by-digit using basic decimal math.
Function ConvertTo(S$,Base) rem assumed 32bit integers Total =0 Negate=0 for lp=1 to len(s$) Total=Total*10 ThisCHR = asc(mid$(s$,lp)) if ThisChr = asc("-") then Negate=1 if ThisChr >= asc("0") and ThisCHR<=asc("9") Total=Total+(ThisCHR-Asc("0")) endif next if Negate then Total *= -1
What’s happening here?
• Each digit is multiplied into place using base-10 math
• `ASC()` is used to convert characters into numeric values
• The minus symbol `"-"` is detected and applied at the end
This is essentially how a basic `Val()` function works internally.
Step 2: Preparing for Base Conversion
Each output base is selected using bit grouping.
select base case 2 Shift=1 Characters$="01" case 8 Shift=3 Characters$="01234567" case 16 Shift=4 Characters$="0123456789ABCDEF" endselect
Why these values?
• Binary uses 1 bit per digit
• Octal uses 3 bits per digit
• Hexadecimal uses 4 bits per digit
Step 3: Bitwise Conversion Loop
Now the number is converted using bit masking and bit shifting.
if Shift Mask = (2^Shift)-1 Digits = 32 / Shift For lp=0 to Digits-1 ThisCHR = Total and MASK Result$ = Mid$(Characters$,ThisChr+1,1) + Result$ Total = Total >> Shift next endif EndFunction Result$
Important notes:
• Output is a fixed 32-bit representation
• Leading zeros are expected and correct
• Negative numbers are shown using two’s complement
The result string is built from right to left because the least-significant bits are processed first.
Summary
This tutorial demonstrates:
• Manual string → integer conversion
• Decimal positional maths
• Bit masking and shifting
• Why binary, octal and hex exist
• How CPUs naturally represent numbers
This approach may not be the shortest, but it clearly shows how the conversion works under the hood — making it ideal for learners.
Complete Code:
s$="87654321" print s$ +"="+ ConvertTo(S$,2) print s$ +"="+ ConvertTo(S$,8) print s$ +"="+ ConvertTo(S$,16) print "" s$="-12345678" print s$ +"="+ ConvertTo(S$,2) print s$ +"="+ ConvertTo(S$,8) print s$ +"="+ ConvertTo(S$,16) print "" s$="255" print s$ +"="+ ConvertTo(S$,2) print s$ +"="+ ConvertTo(S$,8) print s$ +"="+ ConvertTo(S$,16) print "" Sync waitkey Function ConvertTo(S$,Base) rem assumed 32bit integers Total =0 Negate=0 for lp=1 to len(s$) Total =Total*10 ThisCHR = mid(s$,lp) if ThisChr = asc("-") then Negate=1 if ThisChr >= asc("0") and ThisCHR<=asc("9") Total=Total+(ThisCHR-Asc("0")) endif next if Negate then Total *= -1 Characters$ ="0123456789ABCDEF" select base case 2 Shift=1 case 8 Shift=3 case 16 Shift=4 endselect if Shift Mask =(2^Shift)-1 For lp=1 to 32 / Shift ThisCHR = Total and MASK Result$ = Mid$(CHaracters$,ThisChr+1,1) +Result$ Total = Total >> Shift next endif EndFunction Result$




