Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to encode a 7 character long string into a 5 character long string

Status
Not open for further replies.

converge

Programmer
Jul 10, 2002
15
US
I have a series of strings that are 7 characters long each. My problem is this: these strings contain important product information that needs to be encoded. However these strings need to be encoded into 5 character long strings and encoded in such a way that just by reading the string values don't give away valuable information. The process needs to be reversable (convert the encoded 5 character long strings into 7 characters long). Thanks.
 
Shouldnt be that difficult unless you use the full 256 characters. First determine the maximum number of characters... if that value exceeds 127 then there will be no way to do it because you will need the full 8 bits. However if it is 127 you will only need 7 bits of encoding. for a 7 character long string this will bring you down to 6 character encoding of a 7 character string. Still not ideal. What you really need to do is figure out how to encode it as 63 characters or less. This will provide you with a 6 bit encoding of 8 bit values. A remapping of what each value is will be needed. If it is just the character set

"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" we have 52 characters. That leaves space for 11 more characters which could include punctuation etc.

Once the character set is determined, binary operations or 1 to 1 mapping will be required.

I hope this helps.

Matt
 
Actually, thinking about this... you cant even use 63 characters. Here is the math

8 bits * 7 = 56 bits over all
8 bits * 5 = 40 bits over all

To encode this you will have a max cap of 40 bits instead of 56

40/7 (rounded down) = 5

5 bits gives a max encoding of 31 characters. Now there are ways around it. If a specific sequence occurs every time then that sequence can be encoded instead. Also, there is another approach where a key is passed with the string to decode it. Are you limited to 40 bits? Also remember that there will be an EXTRA 5 bits left over from the encoding... You could encode a sequence in those extra 5 bits to decode with.

Matt
 
thanks for your reply. it turns out now that i only need to use 4 characters of the 7 character string for encoding. so now i only need to encode 4 character long strings into 5 character long strings. i'm using the character set "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
 
i can just do a simple one-to-one mapping scheme now. there is a good chance no one will even see the "encoded strings" but if someone stumbled across them, it should not be immediate as to what they really are.
 
my original solution was to add up all the ascii values of each character and take the hex value of that number. however this doesn't always assure unique numbers.
 
Oh... going larger to smaller can cause problems but from smaller to larger you can do plenty

8 bits * 4 = 32
8 bits * 5 = 40

each character can get an extra 2 bit encoding either at the beginning,end, or specific bits like bit 3 and bit 6. you can do this with a simple shift opperation. The first 2 bits can be used as dont care bits or allow you to encode it completely different. Check the binary for beginners FAQ i wrote up if you need help with binary operations.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top