Base conversion code - Comments, please!

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply

Do you think this code would be useful?

Yes
1
20%
No
0
No votes
Might be...
3
60%
HEX$ and OCT$ work just fine for me, thank you...
1
20%
 
Total votes: 5

Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Base conversion code - Comments, please!

Post by Patz QuickBASIC Creations »

Code: Select all

FUNCTION Dec& (Convert$, NumBase%)
'
' Converts numbers from other bases into Base 10 (decimal). Returns 
' -1 if  an invalid character was used for the base used. Returns 
' -2 if you tried  to use an invalid base. (Range: Base 2-Base 36).
' Else returns the number you specified from the base you specified 
' into decimal.
'
' Ex. To convert 9D1CB from hexadecimal (base 16) to decimal:
'               Dec%("9D1CB",16)
'
Convert$ = LTRIM$(RTRIM$(UCASE$(Convert$)))
IF 2 > NumBase% OR NumBase% > 36 THEN Dec& = -2: EXIT FUNCTION
FOR a = 1 TO LEN(Convert$)
Current$ = MID$(Convert$, a, 1)
IF ASC(Current$) > 64 AND ASC(Current$) <91> 47 AND ASC(Current$) <58>= NumBase% THEN Dec& = -1: EXIT FUNCTION
Storage& = Storage& + Cur% * (NumBase% ^ (LEN(Convert$) - a))
NEXT a
Dec& = Storage&
END FUNCTION

Code: Select all

FUNCTION FromDec$ (DecNum&, NumBase%)
'
' Converts a decimal number (base 10) to another base 
' of your choice. Returns -1 if the base is out of range. 
' (Range: Base 2-Base 36)
'
' Ex. To convert 643531 into hexadecimal (base 16):
'                FromDec$(643531, 16)
'
' Note: This works like OCT$ and HEX$, except this works 
' for any base (2-36) instead of just base 8 (octal) and base 
' 16 (hexadecimal).
'
'    So...    HEX$(Number%) = FromDec$(Number%, 16)
'             OCT$(Number%) = FromDec$(Number%, 8)
'
IF 2 > NumBase% OR NumBase% > 36 THEN FromDec$ = "-1": EXIT FUNCTION
DO
Try& = NumBase% ^ a
IF DecNum& <Try> 9 THEN
	LET Storage$ = Storage$ + CHR$(Temp2& + 55)
ELSE
Storage$ = Storage$ + LTRIM$(RTRIM$(STR$(Temp2&)))
END IF
DecNum& = Temp1&
NEXT B
IF LEN(Storage$) > 1 THEN LET Storage$ = RIGHT$(Storage$, LEN(Storage$) - 1)
FromDec$ = Storage$
END FUNCTION
Is there anything you can find wrong with these codes? Memory leaks, bugs, etc? There is only one I can really find (and roughly fixed) and that is that Dec& will sometimes turn negative...? I guess it "overflows", but not as in a compiler error... Help?
Last edited by Patz QuickBASIC Creations on Thu Feb 07, 2008 6:10 pm, edited 1 time in total.
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Re: Base conversion code - Comments, please!

Post by moneo »

Patz QuickBASIC Creations wrote: ......
Is there anything you can find wrong with these codes? Memory leaks, bugs, etc? There is only one I can really find (and roughly fixed) and that is that Dec& will sometimes turn negative...? I guess it "overflows", but not as in a compiler error... Help?
I ran a few tests with "normal" numbers, and they seem to work fine. For large numbers, I didn't get a negative, I got an overflow error. Maybe you could figure out what the maximum numbers that your functions can handle, and either add a test for these, or define these limits in the instructions.

Nice work! What you would use these for, who knows? But, stash them away so that when you ever need them, you've got them.

Regards..... Moneo
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations »

One thing I did before was this:
I tried to open the TI-84 Plus grpahing calculator's operating system in a hex editor, in order to see if there was something I could mess around with. What I found out is this:

Instead of being written in hexadecimal like this: (as viewed as ASCII text)

Code: Select all

}╞♫
It was written as a plaintext document like this:

Code: Select all

7DC60E
So, I made a program to parse all of these plaintext-hexidecimals into machine-code-hexidecimal. I finally found what I was looking for, modified the code, and put it back into the plaintext-hexadecimal format. I sent it to my calculator, and viola! all of my changes had taken place. Just one example. (I got the idea for these from Omnicalc on the TI83+/TI84+)
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations »

About the negative numbers, try this:

Code: Select all

PRINT Dec&("BDYSAUD", 36)
That gives you a negative number because (I'm guessing) it "overflows" the 2,147,***,*** limit. That's what I meant by that.

As for getting an actual overflow error, I have found what you mean. (FromDec$) I'm trying to figure out a less errory (sic) core to find the maximum power to use. So far I'm not having luck :?
comperr
Newbie
Posts: 4
Joined: Mon Dec 18, 2006 1:10 pm

Post by comperr »

useful.
Post Reply