Base convert function alpha

User avatar
sarossell
Posts: 195
Joined: Sat Nov 05, 2016 6:31 pm
My devices: iPad Mini 2, iPhone 5, MacBook Air, MacBook Pro
Flag: United States of America
Contact:

Base convert function alpha

Post by sarossell »

I started working on a number converter to convert any number from base 2 to base 64, but I didn't get very far. It works great as long the numbers are actual comprised of numerals, but as soon as letters get involved, it falls apart. Thought I;d put this out there to see if I'm barking up the wrong tree with this approach. It's clearly not yet worthy of the libraries subsection, but I hope to figure it out soon.

Code: Select all

num$=BASE$(255,10,16)
PRINT num$&":";LEN(num$);"digits long."
END

DEF BASE$(n,f,t)	'(n)umber (f)rom base (t)o base
	OPTION BASE 1
	s$="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#%"
	DEC$=""
	DO
    		temp=INTEG(n%t)
    		DEC$=MID$(s$,(temp+1),1)&DEC$
    		n=INTEG(n/t)
	UNTIL n=0
	OPTION BASE 0
	RETURN DEC$
ENDDEF
smart BASIC Rocks!

- Scott : San Diego, California

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

Re: Base convert function alpha

Post by Henko »

I suppose that the first parameter must be in the "from" base format, hence it must be a string variable ("from" may be larger than 10).
I would the convert it into a base 10 number and from there into a base "to" number.

Another point: the function may be called from a program that uses "option base 1" . Your function then changes this to base 0 with possible disasterous results :lol: .

So, at the start of the function, put

save_base=OPTION_BASE() ! OPTION BASE 1

And at the end of your function :

OPTION BASE save_base

Joel
Posts: 57
Joined: Fri Jan 15, 2016 1:36 pm
My devices: miniipad
Flag: Germany

Re: Base convert function alpha

Post by Joel »

I think you need a second function to convert your input to decimals...
Here my recursive proposal which is independent from OPTION_BASE()

Bye, Joel

Code: Select all

num$=BASE$("FF",16,10)
PRINT num$&":";LEN(num$);"digits long."
END

DEF BASE$(n$,f,t)   '(n)umber (f)rom base (t)o base
   OPTION BASE 1
   n=base2dec(n$,f)
   s$="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#%"
   DEC$=""
   DO
          temp=INTEG(n%t)
          DEC$=MID$(s$,(temp+1),1)&DEC$
          n=INTEG(n/t)
   UNTIL n=0
   OPTION BASE 0
   RETURN DEC$
ENDDEF 

DEF base2dec(num$,from_base)
 s$="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#%"
 n=INSTR(s$,MID$(num$,OPTION_BASE(),1))-OPTION_BASE()
 IF LEN(num$)=1 THEN
  RETURN n
 ELSE 
  RETURN n*(from_base^(LEN(num$)-1)) + base2dec(RIGHT$(num$,LEN(num$)-1),from_base)
 ENDIF  
END DEF

Last edited by Joel on Fri Jan 06, 2017 4:38 pm, edited 1 time in total.

User avatar
sarossell
Posts: 195
Joined: Sat Nov 05, 2016 6:31 pm
My devices: iPad Mini 2, iPhone 5, MacBook Air, MacBook Pro
Flag: United States of America
Contact:

Re: Base convert function alpha

Post by sarossell »

Ah! Clever. I didn't realize OPTION_BASE () existed. Useful, that. Thanks Joel.

I'm thinking I'll have to include a bit of error checking as well to avoid trying to convert a number that includes digits out of the range in the base designated - like converting 9F from Octal base 8. Nope. :D
smart BASIC Rocks!

- Scott : San Diego, California

User avatar
sarossell
Posts: 195
Joined: Sat Nov 05, 2016 6:31 pm
My devices: iPad Mini 2, iPhone 5, MacBook Air, MacBook Pro
Flag: United States of America
Contact:

Re: Base convert function alpha

Post by sarossell »

Henko wrote:I suppose that the first parameter must be in the "from" base format, hence it must be a string variable ("from" may be larger than 10).
I would the convert it into a base 10 number and from there into a base "to" number.
Sure enough. You're right, of course. Thanks.

I may have gotten in over my head here, but it'll be fun to tread water for a while. :shock:
smart BASIC Rocks!

- Scott : San Diego, California

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: Base convert function alpha

Post by GeorgeMcGinn »

That's some great piece of coding. Don't ever give up. Anyone here will help you.

In my career I added statements and functions to the compilers of PL/1 and FORTRAN (written in assembler and machine code) when I worked in research to help programmers and clinicians who could program write fewer lines for some heavy duty features. I even added new functions in SAS and made it easier for SAS code to compile (inline) within PL/1.

And using Henko's Hex program to help me write an EBCDIC base-n Hex Calculator I fouled it up due to a real rookie mistake.

Thanks.

George.

sarossell wrote:
Fri Jan 06, 2017 4:35 pm
Henko wrote:I suppose that the first parameter must be in the "from" base format, hence it must be a string variable ("from" may be larger than 10).
I would the convert it into a base 10 number and from there into a base "to" number.
Sure enough. You're right, of course. Thanks.

I may have gotten in over my head here, but it'll be fun to tread water for a while. :shock:
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)

User avatar
sarossell
Posts: 195
Joined: Sat Nov 05, 2016 6:31 pm
My devices: iPad Mini 2, iPhone 5, MacBook Air, MacBook Pro
Flag: United States of America
Contact:

Re: Base convert function alpha

Post by sarossell »

Thanks very much for the encouragement. I figure that an incoming number need not be defined by base since it's digits will self-determine the upper range. I've also been thinking that in order to avoid smart BASIC's 53 bit base-10 number limitation, I may have to pre-convert to binary in an arbitrarily long string variable instead of decimal with its limitations.

I set this aside for a bit in order to focus on the basics I was familiar with in other versions of BASIC. For example, I've been trying to figure out how to print inverse and flashing text within a sentence using defined functions like INVERSE() and FLASH(). Yet again though, I have no idea how. But it will be fun to figure it out.

Fear not, I don't give up. In fact, I've been working on a particular encryption/compression problem since 1998.
smart BASIC Rocks!

- Scott : San Diego, California

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: Base convert function alpha

Post by GeorgeMcGinn »

You're welcome, We all have our unique talents, and what I can't see others do and vise versa. That's why we have these forums. To share ideas and help each other if we can.

Like some of the stuff that Dutchman, Henko and Ricardo and I have been talking about input fields has helped me greatly as I am writing a parser for both JSON and XML files. I was a consultant to Verizon-TSI and GSM (those in Europe should be very familiar with who GSM is), as I am the reason they were able back then to use their cell phones to purchase soda and beer at vending machines with their (at the time) GSM phones, among other items like groceries and movie tickets.

I helped them perfect their TAP3 file processing. It started out as XML and my senior analyst and I devised a way to encode it into a binary stream and decode it on our end for processing. It was a great project. We wrote most of it in Assembler. Back then it cost a ton to transmit data and even use the Internet, so time online was important and using binary with compression shortened file sizes.

So let me know if you need help with pre-converting to binary strings. Lua has the same limitation of 2^53 (has to do with 64-bit floating point) and Lua as with SB are both written in C/C++, which if I can recall the thread we had this discussion before, it is a hardware math processor limitation. Most languages layoff their math to a math co-processor because it is faster than writing all that assembler code or C/C++ code to do formulas.

I am still looking for a way to do large number math. Towards the end of last year I helped a scientist with her peer-review paper on leap seconds for the ITU, and then she wrote a book on KOBO. I proofed all the math and science and got a nice acknowledgement in her book. This is just some of the stuff I do in Cosmology/Physics and I hate doing Trig and Calc longhand.

And I was unpleasantly surprised when helping another astronomer with a paper on temperatures of black holes. It was the first time I had used SmartBASIC for something this important and the numbers were all wrong.

The code is below, but this is to show you why it is important to have a string format that preserves the precision of all integers and decimals.

Our manual is still wrong. We are able to do 2^53, not 2^52. And below I also threw in what happens when you decide to show all 30 integers. For example, the mass of the Earth is about 2*10^30. That's 2 with 30 zeroes. Below is what happens in SmartBASIC:
The maximum absolute value 2^52 = 4,503,599,627,370,496
Add 4503599627370496 to itself = 9,007,199,254,740,992
The maximum absolute value 2^53 = 9,007,199,254,740,992
The mass of the Sun, 2*10^30 = 2000000000000000039769249677312
As an exponent, the Sun's Mass. = 2E+30
Look at all those non-zero numbers inserted. For most all uses, 9,007,199,254,740,992 is plenty big enough. But when I coded my formulas and even when it did the math in exponential form, because of the 7-digit mantissa, it looked like the results were right, but at the end they were not because of those non-zeroes were inserted even in exponential calculations.

So if you need a hand with creating a similar string format that Lua and some others have to preserve every integer no matter how long the number is, let me know. I am in need of a solution (for personal work) and I have been searching and learning new languages because of it, which is not a bad thing.

When I started my career in 1975, I thought assembler and machine code was all I would need. Once I learned COBOL, PL/1, PL/C (Caltech's version based on PL/1), RPG and many others, it allowed me to take the best of each and when needed I was able to write my own functions, preprocessors/precompiler code, inline preprocessor (such as allowing SAS code to compile in a PL/1 program) and many other things needed by others.

If you need help, let me know, and also I bet many here would also help as they too would like to do precision long integer math.

George.

PS: The reference manual needs to read 2^53, not 52.

Code: Select all

X=2^52
Y=2*10^30
Z=2^53
W=4503599627370496+4503599627370496

F16$="#,###,###,###,###,###"
F20$="##,###,###,###,###,###,###"
F30$="##############################"


X$=STR$(X,F16$)
Y$=STR$(Y,F30$)
Z$=STR$(Z,F16$)
W$=STR$(W,F16$)

PRINT "The maximum absolute value 2^52 = "&X$
PRINT "Add 4503599627370496 to itself  = "&W$
PRINT "The maximum absolute value 2^53 = "&Z$
PRINT "The mass of the Sun, 2*10^30    = "&Y$
PRINT "As an exponent, the Sun's Mass. = "&Y

STOP
sarossell wrote:
Mon Jan 16, 2017 4:48 am
Thanks very much for the encouragement. I figure that an incoming number need not be defined by base since it's digits will self-determine the upper range. I've also been thinking that in order to avoid smart BASIC's 53 bit base-10 number limitation, I may have to pre-convert to binary in an arbitrarily long string variable instead of decimal with its limitations.

I set this aside for a bit in order to focus on the basics I was familiar with in other versions of BASIC. For example, I've been trying to figure out how to print inverse and flashing text within a sentence using defined functions like INVERSE() and FLASH(). Yet again though, I have no idea how. But it will be fun to figure it out.

Fear not, I don't give up. In fact, I've been working on a particular encryption/compression problem since 1998.
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)

User avatar
sarossell
Posts: 195
Joined: Sat Nov 05, 2016 6:31 pm
My devices: iPad Mini 2, iPhone 5, MacBook Air, MacBook Pro
Flag: United States of America
Contact:

Re: Base convert function alpha

Post by sarossell »

I'm very jealous of your background and work. Sadly, I've battled a health condition since I was eight that prevented me from finishing college and my career in the Air Force. My pace is slow but deliberate and unceasing. So I may eventually get there, but it will take me much longer than most others.

Thanks again for the encouragement and information. As soon as I figure out how to print text in inverse and flashing, I'll return to the base converter program. The only way that comes to mind is through HTML/CSS within a browser.
smart BASIC Rocks!

- Scott : San Diego, California

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: Base convert function alpha

Post by GeorgeMcGinn »

No problems. I understand health issues. I'm still paying physically for a car accident in 1998. Just this Friday while needing a walker I fell down the slope of a 30-foot swale. I've had 3 back surgeries, all failed and hospitalized 10 times for MRSA sepsis, including 5 comas. And now I need my right hip fused.

This also slows my development down as I have three very intense projects that I am using both SB and Swift for. I haven't tried it yet, but I am finishing a small test to see if through their SDK's and XCode development I can get one to talk to the other, even if they pass notes via clipboard.

You can read parts of my condition here: viewtopic.php?f=20&t=1624

But like you, it takes me a lot longer to do things, with the pain, the meds, the inability to move. I had my neighbor move my iMAC to my bedroom so I could easily access it.

When you are ready, let me know.

Sorry you missed your career in the Airforce. I spent 3 years active, 4 inactive reserve as a combat MP/SRT. I should have done 20 and I would still have done some computer work, but with a full military pension.

Take care of yourself. George
sarossell wrote:
Tue Jan 17, 2017 2:10 am
I'm very jealous of your background and work. Sadly, I've battled a health condition since I was eight that prevented me from finishing college and my career in the Air Force. My pace is slow but deliberate and unceasing. So I may eventually get there, but it will take me much longer than most others.

Thanks again for the encouragement and information. As soon as I figure out how to print text in inverse and flashing, I'll return to the base converter program. The only way that comes to mind is through HTML/CSS within a browser.
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