12
« on: October 05, 2021, 12:52:21 am »
This Binary function used to work just fine, until the latest dev update I installed today.
_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
Who was the helpful soul who wrote this? Bplus? I don't remember, but it wasn't Steve. Now I get this blocking error message, which refers to this line highlighted in red:
If (temp~&& And 1) Then Mid$(Bin$, charPos%, 1) = "1": highPos% = charPos%
"Incorrect number of arguments passed to function on line 45.
Caused by (or after) MID$ ( BIN$, CHARPOS% , 1) = "1",1 "
Sorry, I don't know what that means, nor why suddenly there should be this fatal error? (I'm sure it's obvious to most decent programmers.)
Thanks!