Page 1 of 2
Decimal to Hex Function return
Posted: Tue Feb 12, 2019 4:50 pm
by Speedbart
Hi, I am new to SmartBasic (but with 35 years programming experience in other stuff) and I love it - thank you!!!
But...I am going nuts trying to understand why the function below refuses to return any result that has a non-numeric character even though all returns are strings. So "13" comes back but "A3" won't and nor will "3A" - any ideas welcome!
(Also I wasn't able to find MOD() function so I have coded an equivalent)
'------------------------------------------------
For X=0 to 50
Print Dec2Hex(X)
Next X
'------------------------------------------------
Def Dec2Hex(N)
N1=Floor((N/16))
N2=N-Floor((N1*16))
IF N1=0 THEN
H1$="0"
Else
If N1>9 Then
H1$=Chr$(55+N1)
Else
H1$=Str$(N1,"#")
ENDIF
Endif
IF N2=0 THEN
H2$="0"
Else
If N2>9 Then
H2$=Chr$(55+N2)
Else
H2$=Str$(N2,"#")
Endif
Endif
' This refuses to return any of the non numeric strings!!!!
Return (H1$ & H2$)
End Def
'------------------------------------------------
Re: Decimal to Hex Function return
Posted: Tue Feb 12, 2019 4:58 pm
by Henko
Hi,
Mod function is the % sign : 5 % 3 = 2
Re: Decimal to Hex Function return
Posted: Tue Feb 12, 2019 5:07 pm
by Henko
Second issue:
If a function needs to return a string, then the function name must end with a $ sign too.
Hence: def num2hex$(num) or likewise
Re: Decimal to Hex Function return
Posted: Tue Feb 12, 2019 6:27 pm
by rbytes
Welcome to the Forum, Speedbart.
You will find lots of help and examples here.
Re: Decimal to Hex Function return
Posted: Tue Feb 12, 2019 6:46 pm
by matt7
Henko wrote: ↑Tue Feb 12, 2019 5:07 pm
If a function needs to return a string, then the function name must end with a $ sign too.
I struggled to figure this out myself a while ago. This really should have been explained in the documentation on the "Basic" page under "User Functions." (I pretty much only use the in-app documentation, but maybe this is already in the PDF version that gets updated by Dutch.)
Re: Decimal to Hex Function return
Posted: Tue Feb 12, 2019 6:54 pm
by Mr. Kibernetik
matt7 wrote: ↑Tue Feb 12, 2019 6:46 pm
Henko wrote: ↑Tue Feb 12, 2019 5:07 pm
If a function needs to return a string, then the function name must end with a $ sign too.
I struggled to figure this out myself a while ago. This really should have been explained in the documentation on the "Basic" page under "User Functions." (I pretty much only use the in-app documentation, but maybe this is already in the PDF version that gets updated by Dutch.)
Smart BASIC in-app documentation is not a full description of BASIC language syntax (which is a big book itself). It is more a documentation of new smart BASIC additions to Minimal BASIC language syntax.
Re: Decimal to Hex Function return
Posted: Tue Feb 12, 2019 8:44 pm
by Speedbart
Hi Guys thank you sooo much for the prompt replies - I tried adding the $ and it now works perfectly (it's an application to change the colour of an LED on an IOT device).
I just love this language you have developed, I cannot imagine how much work is involved or actually how you did it but it's just the best, thanks again!
I have found a really simple work around for the DropBox issue (which stopped working for me when I changed my DropBox details) and I will post then under the correct topic when I get a moment.
Yahoo!
Re: Decimal to Hex Function return
Posted: Tue Feb 12, 2019 8:47 pm
by Speedbart
rbytes wrote: ↑Tue Feb 12, 2019 6:27 pm
Welcome to the Forum, Speedbart.
You will find lots of help and examples here.
Thanks, yes, I'm dead impressed so far - this is now my favourite app - I was about to slog through Swift and Xcode which I can now put off for a while
Re: Decimal to Hex Function return
Posted: Tue Feb 12, 2019 8:58 pm
by matt7
Mr. Kibernetik wrote: ↑Tue Feb 12, 2019 6:54 pm
Smart BASIC in-app documentation is not a full description of BASIC language syntax (which is a big book itself). It is more a documentation of new smart BASIC additions to Minimal BASIC language syntax.
Ah, thanks for that clarification. I didn't even know until your recent post about the new Basic language you are working on that smart BASIC was built on Minimal BASIC. That's good to know in case I run into something that the in-app documentation isn't giving me much info on.
Re: Decimal to Hex Function return
Posted: Sat Feb 16, 2019 11:31 pm
by GeorgeMcGinn
Why does everyone like doing things the hard way?
HEX$ (N)
returns hexadecimal representation of integer number [n]. Number should not be negative or complex.
PRINT HEX$(255)
This code does the same thing with very little risk for bugs:
Code: Select all
FOR X=0 TO 255
PRINT HEX$(X)
NEXT X