Author Topic: [TI-8x] Help Optimizing Conversion Program  (Read 2710 times)

0 Members and 1 Guest are viewing this topic.

Offline XVicarious

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +45/-28
  • I F**king Love Twisty Puzzles
    • View Profile
    • XVicarious
[TI-8x] Help Optimizing Conversion Program
« on: June 30, 2011, 09:13:38 pm »
I really didn't know how to do this, so I did it the long way, how could optimizing this be done? Its to convert Octodecimal (Base 18) to Decimal (Base 10):

Code: [Select]
length(Str1)->O
For(I,1,O)
sub(Str1,I,1)->Str2
If (Str2="0")
R+(0*18^(O-I))->R
If (Str2="1")
R+(1*18^(O-I))->R
If (Str2="2")
R+(2*18^(O-I))->R
If (Str2="3")
R+(3*18^(O-I))->R
If (Str2="4")
R+(4*18^(O-I))->R
If (Str2="5")
R+(5*18^(O-I))->R
If (Str2="6")
R+(6*18^(O-I))->R
If (Str2="7")
R+(7*18^(O-I))->R
If (Str2="8")
R+(8*18^(O-I))->R
If (Str2="9")
R+(9*18^(O-I))->R
If (Str2="A")
R+(10*18^(O-I))->R
If (Str2="B")
R+(11*18^(O-I))->R
If (Str2="C")
R+(12*18^(O-I))->R
If (Str2="D")
R+(13*18^(O-I))->R
If (Str2="E")
R+(14*18^(O-I))->R
If (Str2="F")
R+(15*18^(O-I))->R
If (Str2="G")
R+(16*18^(O-I))->R
If (Str2="H")
R+(17*18^(O-I))->R
End

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: [TI-8x] Help Optimizing Conversion Program
« Reply #1 on: June 30, 2011, 09:20:09 pm »
Well, if the character in the string is a [decimal] number, then you can just use expr(Str2) to get the digit, so what you want to do is put the code for A-H first and if none of those match, use expr. Oh, and you can use else ifs instead of all but the first if.

Offline XVicarious

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +45/-28
  • I F**king Love Twisty Puzzles
    • View Profile
    • XVicarious
Re: [TI-8x] Help Optimizing Conversion Program
« Reply #2 on: June 30, 2011, 09:34:18 pm »
Thanks Runer112, your code works so much (in IRC)

Code: [Select]
Input "Octodec: ",Str1
length(Str1)->O
For(I,1,O)
inString("0123456789ABCDEFGH",sub(Str1,I,1))-1->A
(A*18^(O-I))+R->R
End
Disp R
Pause

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: [TI-8x] Help Optimizing Conversion Program
« Reply #3 on: June 30, 2011, 09:34:46 pm »
Code: [Select]
0
For(I,1,length(Str2
18Ans-1+inString("0123456789ABCDEFGH",sub(Str2,I,1
End
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Re: [TI-8x] Help Optimizing Conversion Program
« Reply #4 on: June 30, 2011, 09:34:49 pm »
A slightly cheap way that I use is make a string called "0123456789ABCDEFGH" then use another for loop with sub( to find it in that string, then that should be the equivalent number.

EDIT: ninja'd
« Last Edit: June 30, 2011, 09:35:14 pm by leafiness0 »
In-progress: Graviter (...)

Offline XVicarious

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +45/-28
  • I F**king Love Twisty Puzzles
    • View Profile
    • XVicarious
Re: [TI-8x] Help Optimizing Conversion Program
« Reply #5 on: June 30, 2011, 09:36:33 pm »
Wow. Super optimization.