Manual Base Conversion in PlayBASIC

December 08, 2025

 

Logo

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$

Remember when a whole generation of kids kick-started the games industry by digging through 8-bit hardware manuals

November 18, 2025

 

Logo

Remember when a whole generation of kids kick-started the games industry by digging through 8-bit hardware manuals?

I do.

Back then, we made things simply because coding was how you made things.

Nobody cared how it looked on a CV. Nobody waited for permission, or worried about “best practices” or “the perfect engine.” We experimented. We shared ideas. We read magazines (ask your parents what a magazine is!). We broke things and fixed them. We layered idea upon idea until the impossible suddenly wasn’t.

People called it talent.

It wasn’t.

It was curiosity, consistency, and a methodical approach to building skills.

Today? We’re drowning in information. Thousands of tutorials, hundreds of languages, endless opinions echoing in your head: Do this. Don’t do that. You must learn this first. No, learn that.

It’s no wonder beginners freeze before they even start.

But here’s the truth:

If you want to learn to code, pick a language — any language — and give it a shot.

The specific language matters far less than people claim.

Once you understand the basics in one, those skills transfer. Moving to another becomes easier. Concepts repeat. Patterns reappear. You build momentum.

Start small.

Be proud of the little victories.

And if your first attempt doesn’t stick? That’s normal. Try again later. You’ll be surprised how much your brain held onto.

Where you start isn’t where you’ll finish — and that’s the whole point.

Just keep going!

#LearnToCode #CodingJourney #ProgrammingMotivation #GameDevBeginners #CodeNewbies #StartCoding #KeepCoding #ProgrammingLife #RetroCoding #OldSchoolComputing #IndieDevLife #GameDevCommunity #SoftwareDevelopment #DeveloperMindset #ProgrammingBasics #CodingTips #CodeEveryday #STEMEducation #TechInspiration #GamedevHistory #8bitComputing #CreativeCoding #BuildInPublic #FutureDevelopers


You Know the Problem with Programming Forums…?

November 03, 2025

 

Logo

You know the problem with programming forums?

It’s not the questions. It’s not even the bugs. It’s the people.

Everywhere you go, there’s a small but mighty group of seasoned coders who really get it — the ones who’ve wrestled with obscure compiler errors, traced logic bugs through 200 lines of nested loops, and still have the mental energy left to explain it to someone else (usually with more patience than the situation deserves).

These are the quiet heroes of the community — they drop by, solve your issue with a three-line code sample, and vanish into the digital mist like some kind of stack-trace samurai.

And then… there’s the other group.

The overconfident few who, thanks to the magic of the Dunning–Kruger effect, have somehow achieved total certainty with only partial understanding. They appear out of nowhere, ready to tell you how your code should work — and they’ll do it with such conviction that you start questioning your own sanity.

It’s a fascinating phenomenon:

The less someone knows, the more aggressively they’ll argue about it.

They’ll cite “best practices” they half-remember, invent terminology mid-sentence, and copy–paste code that looks like it was written during a caffeine overdose at 3 a.m. in 1998.

And because confidence sounds like competence online, the myth spreads faster than a memory leak in an infinite loop.

Meanwhile, the real experts quietly bow out of the conversation, sensing that no amount of logic will change the outcome. They’ve seen it before. They know the cycle: ask → argue → ego → silence → repeat.

Now don’t get me wrong — every programmer starts somewhere, and we’ve all been wrong (some of us spectacularly so). But there’s a big difference between learning and lecturing. The first builds communities. The second just fills them with noise.

Maybe the cure is humility — or at least a sense of humour about how we’re all wrong sometimes. If we could just admit that once in a while, maybe the next generation of coders wouldn’t have to scroll through ten paragraphs of “expert advice” before finding the one post that actually fixes the problem.

Until then, I’ll be here, sipping coffee, watching the cycle repeat — and quietly hoping someone, somewhere, finally invents a compiler for forum advice.