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
'------------------------------------------------
Decimal to Hex Function return
-
- Posts: 814
- Joined: Tue Apr 09, 2013 12:23 pm
- My devices: iPhone,iPad
Windows - Location: Groningen, Netherlands
- Flag:
Re: Decimal to Hex Function return
Hi,
Mod function is the % sign : 5 % 3 = 2
Mod function is the % sign : 5 % 3 = 2
-
- Posts: 814
- Joined: Tue Apr 09, 2013 12:23 pm
- My devices: iPhone,iPad
Windows - Location: Groningen, Netherlands
- Flag:
Re: Decimal to Hex Function return
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
If a function needs to return a string, then the function name must end with a $ sign too.
Hence: def num2hex$(num) or likewise
- rbytes
- Posts: 1338
- Joined: Sun May 31, 2015 12:11 am
- My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet - Location: Calgary, Canada
- Flag:
- Contact:
Re: Decimal to Hex Function return
Welcome to the Forum, Speedbart.
You will find lots of help and examples here.
You will find lots of help and examples here.
The only thing that gets me down is gravity...
Re: Decimal to Hex Function return
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.)
- Mr. Kibernetik
- Site Admin
- Posts: 4786
- Joined: Mon Nov 19, 2012 10:16 pm
- My devices: iPhone, iPad, MacBook
- Location: Russia
- Flag:
Re: Decimal to Hex Function return
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.matt7 wrote: ↑Tue Feb 12, 2019 6:46 pmI 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.)
-
- Posts: 4
- Joined: Sat Jan 12, 2019 11:17 pm
- My devices: iPhone, iPad, MacBook
- Location: Plymouth UK
- Flag:
Re: Decimal to Hex Function return
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!
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
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.Mr. Kibernetik wrote: ↑Tue Feb 12, 2019 6:54 pmSmart 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.
- GeorgeMcGinn
- Posts: 435
- Joined: Sat Sep 10, 2016 6:37 am
- My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10 - Location: Venice, FL
- Flag:
- Contact:
Re: Decimal to Hex Function return
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:
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
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)