_Title "Test of BIN$ function"
Screen _NewImage(120, 43, 0)
Color 1, 7
Cls
1 Input "Enter decimal (0 exits) -> ", dec
If dec = 0 Then End
binvalue$ = Bin$(dec)
recdec = Val("&b" + binvalue$)
hexvalue$ = Hex$(dec)
Print "Decimal"; dec; " equals binary "; binvalue$; " equals hex "; hexvalue$; " reconstructs to decimal"; recdec
Print
GoTo 1
End
'---------------------------------------------------------------------
'Function: Convert any given dec/hex/oct number into a binary string.
' Can handle positive and negative values and works in that
' similar to the QB64 built-in HEX$ and OCT$ functions.
'
'Synopsis: binary$ = BIN$ (value&&)
'
'Result: binary$ --> the binary representation string of the given
' number without leading zeros for positive values
' and either 8/16/32 or 64 chars for negatives,
' depending on the input size
'
'Inputs: value&& --> the pos./neg. number to convert, may also be
' given as &H or &O prefixed value
'
'Notes: You may also pass in floating point values, as long as its
' represented value fits into the _INTEGER64 (&&) input, hence
' approx. -9.223372036854776E+18 to 9.223372036854776E+18.
' Different from HEX$ and OCT$, BIN$ won't throw an overflow
' error, if this range is exceeded, but the result is probably
' wrong in such a case.
'---------------------------------------------------------------------
Function Bin$ (value&&)
'--- option _explicit requirements ---
Dim temp~&&, charPos%, highPos%
'--- init ---
temp~&& = value&&
Bin$ = String$(64, "0"): charPos% = 64: highPos% = 64
'--- convert ---
Do
If (temp~&& And 1) Then Mid$(Bin$, charPos%, 1) = "1": highPos% = charPos%
charPos% = charPos% - 1: temp~&& = temp~&& \ 2
Loop Until temp~&& = 0
'--- adjust negative size ---
If value&& < 0 Then
If -value&& < &HFFFFFFFF~& Then highPos% = 33
If -value&& < &H0000FFFF~& Then highPos% = 49
If -value&& < &H000000FF~& Then highPos% = 57
If -value&& < &H00000000~& Then highPos% = 1
End If
'--- set result ---
Bin$ = Mid$(Bin$, highPos%)
End Function
"Careless Looping"
I feel so unsure
As I write this code
And lead you to the dance floor
As my program dies
Something in my eyes
Calls to mind some crappy code
And all its sad loo-oop- ping
I'm never gonna code again
Guilty fingers got no reason
Though it's easy to pretend
I know what I am doing
I should've known better than to write a function
And waste the chance that I'd been given
So I'm never gonna code again
The way I did when I was eleven
Time can never mend
The endless looping of a good function
To the heart and mind
Ignorance is kind
There's no comfort in the truth
Looping is all you'll find
I'm never gonna code again
Guilty fingers got no reason
Though it's easy to pretend
I know what I am do-oo-ing
I should've known better than to write a function
And waste the chance that I'd been given
So I'm never gonna code again
The way I did when I was eleven
I'm never gonna code again
Guilty hands have got no reason
Though it's easy to pretend
I know what I am doing
I should've known better than to write a function
And waste the chance that I'd been given
So I'm never gonna code again
The way I did when I was eleven
Never without your help
Tonight the function must somehow stop
I wish that I could lose this loop
Maybe it's better this way
We'd hurt each other with the code we post online
We could have been so good together
We could have lived this run forever
But now who's gonna stop my code?
Please stop running
I'm never gonna code again
Guilty fingers got no reason
Though it's easy to pretend
I know what I am doing
I should've known better than to write a function
And waste the chance that I'd been given
So I'm never gonna code again
The way I did when I was eleven
Now that code won't end
Now that code won't end
Now that my code done
Was what it I did so wrong, so wrong
That you had to leave me alone?
Recursive functions work now, so only use the function name when you're ready to assign it a return value.
For example, this will now error:
FUNCTION foo$ (a$)
foo$ = _TRIM$(a$)
foo$ = MID$(foo$, 4)
END FUNCTION
the 3rd line now sees the foo$ inside the MID$ as a recursive call back to itself and will error out....
Fix is:
FUNCTION foo$ (a$)
temp$ = _TRIM$(a$)
foo$ = MID$(temp$, 4)
END FUNCTION
*ONLY* use the function name inside the function to assign return values, unless you just want to recursively call the function again.
At this point, I wish we had a simple RETURN (value) command which we could tell folks to use, and then advise them to avoid function names inside a function completely. After all, when you SUB foo, you don’t reference a variable named foo anywhere inside it. ;D
the new official Bin$ is out now https://www.qb64.org/forum/index.php?topic=4261, I strongly recomment you to update, because when I look on your provided code samples, then I see you still use my very old Bin$.
RhoSigma
Well, I'm going crazy. Several things caused me to write new lyrics to "Careless Whispers."
Last night I woke up after watching Rugby on YouTube and low and behold a video done by @FellippeHeitor about subs and functions was playing, and he created an endless loop by calling a function within function. Then I read the post below. I lost it ...