Page **1** of **1**

### Base conversion code - Comments, please!

Posted: **Tue Oct 24, 2006 3:22 pm**

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?

### Re: Base conversion code - Comments, please!

Posted: **Wed Dec 13, 2006 7:03 pm**

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

Posted: **Fri Dec 15, 2006 9:20 am**

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)

It was written as a plaintext document like this:

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

Posted: **Mon Dec 18, 2006 3:01 pm**

by **Patz QuickBASIC Creations**

About the negative numbers, try this:

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

Posted: **Tue Dec 19, 2006 10:35 pm**

by **comperr**

useful.