Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: meishe91 on September 08, 2010, 10:17:04 pm

Title: Word Encoding/Decoding
Post by: meishe91 on September 08, 2010, 10:17:04 pm
Ok, so since I will be including a custom name for your character in the game I'm creating I decided I needed to come up with a way of converting words to a list so they could be stored without the worry of someone changing a string variable. But the only bad part about doing this is that normally you are limited to a seven character limit since normally each letter is two numbers and the calculator only can hold numbers of 14 numbers in length (and then it starts rounding and such, I believe; I just know 14 is the limit :P). So I did some looking around on here and found one topic, I think, that Raylin started that is about this but I wanted to create my own method for this (Raylin's topic is here (http://ourl.ca/3785)).

Now, the code:

Note:
When ever you see "LW" the the code below it is really LW. Same with L2 and L1, but those are pretty well known now. Also, when you see "10^(" it is the actual token that you get when you press [2ND] then [LOG].

Code: (Encode) [Select]
DelVar LWDelVar LW15?dim(L2
DelVarB1?C
Input Str1
iPart((length(Str1)-1)/7+1?dim(LW
For(A,1,length(Str1
10^(B)inString("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz?0123456789",sub(Str1,A,1?L2(B+1
B+2?B
If B=14
Then
sum(L2?LW(C+1
C+1?C
DelVar BDelVar L215?dim(L2
End
End
C?LW(1
sum(L2?LW(C+1

Essentially what this is doing is that it makes a series of numbers based on where they are in the string. When the number gets up to fourteen numbers in length it increases the C variable by one and starts a new number to place in the next element, creating two numbers. Then C is stored to the first element to tell the decoder how many elements the word is.

Now the decoder's code:

Note:
When you see "E2" in the source code that is the scientific notation E.

Code: (Decode) [Select]
"_
For(A,1,LW(1
For(B,2,int(log(LW(A+1)))+2,2
Ans+sub("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz?0123456789",iPart(E2fPart(LW(A+1)10^(-B))),1
End
End
sub(Ans,2,length(Ans)-1?Str1

The thing I like about mine is that, one, the size of the program is basically dependent on what is in that string in the sub( command, two, it is pretty fast if I do say so myself (both the decoder and encoder; obviously though it takes longer with a bigger word) and, three, it is basically all self-working as long as you have the sub( string the way you want. No further math or anything needed (the strings just need to match).

I wouldn't doubt if there are a couple optimizations that could be made to those by the way.

Note:
There are only two downsides to this method. The first is that it is restricted to the 999 elements of a list. I don't forsee this being a huge issue though since you'd need a really long string to do that. (I think it's about 142 tokens but I'm not sure.) The other downside is that the strings that need to match must be under a hundred tokens long. This is because it uses only two numbers to store a token.
Title: Re: Word Encoding/Decoding
Post by: nemo on September 08, 2010, 10:44:19 pm
Code: [Select]
"_
For(A,1,dim(LW
For(B,1,.5+.5int(log(LW(A
Ans+sub("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZzΘ0123456789",iPart(E2fPart(LW(A)/10^(2B))),1
End
End
sub(Ans,2,length(Ans)-1→Str1

i think this still works.. but it's late, and i'm tired, so i'm not completely sure.
Title: Re: Word Encoding/Decoding
Post by: meishe91 on September 08, 2010, 10:52:32 pm
Ya, that actually does seem to work. Thanks. Shaved off six bytes :)
Title: Re: Word Encoding/Decoding
Post by: meishe91 on September 11, 2010, 09:14:39 pm
*Bump*

Did people see this? Just thought this might be useful to some people :)
Title: Re: Word Encoding/Decoding
Post by: DJ Omnimaga on September 11, 2010, 11:47:06 pm
I assume this converts char names to lists? I generally just store my char names into a list, although the way I did it in my old games isn't too efficient, due to each letter taking 1 list element. I would have been better with some compression.
Title: Re: Word Encoding/Decoding
Post by: meishe91 on September 11, 2010, 11:58:03 pm
Well technically this can be used to store any string to a list. It stores every seven tokens (or less) to a element and then goes on to the next element if needed. Then it stores how many elements were used to the first element. I just thought I would provide this since it has no limit on size, besides the list restriction of 999 elements.

But in short, yes, this converts a characters name to a list :P
Title: Re: Word Encoding/Decoding
Post by: DJ Omnimaga on September 11, 2010, 11:58:57 pm
Aaah ok I see :)
Title: Re: Word Encoding/Decoding
Post by: meishe91 on September 12, 2010, 12:03:24 am
Ya. This also isn't limited by to a certain number of tokens that can be used. As long as the strings match you can put what ever you want. Also, the string is basically what decides how big the routine is :D
Title: Re: Word Encoding/Decoding
Post by: DJ Omnimaga on September 12, 2010, 02:35:03 am
Yeah true and since it's TI-BASIC, you can also use the 2 byte tokens. In Axe it can be a major PITA, sometimes.
Title: Re: Word Encoding/Decoding
Post by: meishe91 on September 12, 2010, 02:42:40 am
Ya, I think it'll come in handy. I mean it's a pretty simple routine and method (I think at least).
Title: Re: Word Encoding/Decoding
Post by: meishe91 on September 12, 2010, 02:46:01 pm
I forgot to mention that the only downside about this is that it won't work if your string is over a hundred tokens long. I'll edit that into the first post too.
Title: Re: Word Encoding/Decoding
Post by: DJ Omnimaga on September 12, 2010, 02:50:10 pm
Ah ok I see. I doubt much people would need to use strings that large, though, since it would be at their disadvantage, kinda (memory-wise)
Title: Re: Word Encoding/Decoding
Post by: meishe91 on September 12, 2010, 02:54:26 pm
Ya, that's what I thought too. I don't even think there are a hundred tokens that you would use for typing anyways. But I don't know. Just thought I'd mention it just in case :P
Title: Re: Word Encoding/Decoding
Post by: holmes221b on January 07, 2011, 05:53:47 pm
Can this be used to translate numbers in a matrix into letters?/me is thinking that this could be exactly what she needs for QFIBCODE....
Title: Re: Word Encoding/Decoding
Post by: nemo on January 07, 2011, 06:01:36 pm
Code: [Select]
"_
For(A,1,dim(LW
For(B,1,.5+.5int(log(LW(A
Ans+sub("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZzΘ0123456789",iPart(E2fPart(LW(A)/10^(2B))),1
End
End
sub(Ans,2,length(Ans)-1→Str1

i think this still works.. but it's late, and i'm tired, so i'm not completely sure.

i can't even read that anymore... i'm spoiled by java

and yeah holmes this could be used with some modification for a matrix.
Title: Re: Word Encoding/Decoding
Post by: meishe91 on January 07, 2011, 06:02:06 pm
What do you mean?
Title: Re: Word Encoding/Decoding
Post by: holmes221b on January 07, 2011, 06:06:34 pm
For my cypher program, QFIBCODE, I need something that can take letters and turn them into numbers (and back again) for the user, so they don't have to remember that A=1, B=2, etc. for themselves, the calculator does the translating for them.
Title: Re: Word Encoding/Decoding
Post by: jnesselr on January 07, 2011, 08:24:10 pm
For my cypher program, QFIBCODE, I need something that can take letters and turn them into numbers (and back again) for the user, so they don't have to remember that A=1, B=2, etc. for themselves, the calculator does the translating for them.
Well, let's see, you could do that fairly easy with inString, since it returns the position. So, assuming Str2 was whatever character you wanted to find the number for:
Code: [Select]
inString("ABCDEFGHIJKLMNOPQRSTUVWXYZ",Str2)
Should give you the location, or 0 if Str2 wasn't A-Z
Title: Re: Word Encoding/Decoding
Post by: meishe91 on January 07, 2011, 08:58:52 pm
Ya, that seems like a pretty easy task actually. To go from letter to number you something like Graphmastur said:

Code: [Select]
{4,4→dim([A] \\Using your matrix idea as a basis. I don't know how you have things set up though.
1→B
1→C
For(A,1,length("ABCDFGHIJKLMNOPQ
inString("ABCDEFGHIJKLMNOPQRSTUVWXYZ",sub(Str1,A,1→[A](B,C
B+1-4(B=4→B
C+(B=1→C
End

And to go from number to letter:

Code: [Select]
"_ \\The underscore symbolizes a space.
For(A,1,4
For(B,1,4
Ans+sub("ABCDEFGHIJKLMNOPQRSTUVWXYZ",[A](A,B),1
End
End
sub(Ans,2,length(Ans)-1→Str1

Those both should work, but I just made them on the spot and didn't test them. You might have to flip some things around but the principle remains the same.
Title: Re: Word Encoding/Decoding
Post by: meishe91 on January 11, 2011, 01:51:38 pm
@Holmes
Did this help at all?
Title: Re: Word Encoding/Decoding
Post by: holmes221b on January 11, 2011, 02:06:35 pm
Haven't had a chance to test it yet.