Decimal to Hex Function return

Speedbart
Posts: 4
Joined: Sat Jan 12, 2019 11:17 pm
My devices: iPhone, iPad, MacBook
Location: Plymouth UK
Flag: Great Britain

Decimal to Hex Function return

Post 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
'------------------------------------------------

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Decimal to Hex Function return

Post by Henko »

Hi,
Mod function is the % sign : 5 % 3 = 2

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Decimal to Hex Function return

Post 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

User avatar
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: Canada
Contact:

Re: Decimal to Hex Function return

Post by rbytes »

Welcome to the Forum, Speedbart.

You will find lots of help and examples here.
The only thing that gets me down is gravity...

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: Decimal to Hex Function return

Post 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.)

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Decimal to Hex Function return

Post 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.

Speedbart
Posts: 4
Joined: Sat Jan 12, 2019 11:17 pm
My devices: iPhone, iPad, MacBook
Location: Plymouth UK
Flag: Great Britain

Re: Decimal to Hex Function return

Post 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!

Speedbart
Posts: 4
Joined: Sat Jan 12, 2019 11:17 pm
My devices: iPhone, iPad, MacBook
Location: Plymouth UK
Flag: Great Britain

Re: Decimal to Hex Function return

Post 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 :)

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: Decimal to Hex Function return

Post 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.

User avatar
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: United States of America
Contact:

Re: Decimal to Hex Function return

Post 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
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)

Post Reply