I am attempting to do phase space analysis on Coke Reward Codes that are 14 characters long and composed of only the following 20 characters:

Letters: B F H J K L M N P R T V W X

Numbers: 4 5 6 7 9 0

What I am trying to do is implement a function that takes a string input of a code (ex: "FWR59NR0RR5NHV") and converts it to one unique integer for that code. The reason I need to convert these codes to integers is because I generate an attractor graph by calculating the gradients of a time-series sequence of the codes. So, for example, a small sequence (ordered from newest code to outdated code) may be 7508, 16, 9000, 5200 for the codes "440W6KN40XMBL7," "0PPLV7NW6V9VTJ," "6NB6PL9KXWRV5K," and "5RNB0WJKVRJB0M" respectively. Then, I take the gradients from this sequence and plot them as x, y, and z values as a way to predict a future code (this procedure known as delayed coordinates comes from the experiment located here: http://lcamtuf.coredump.cx/oldtcp/tcpseq.html). The equations for calculating x, y, and z are the following:

 x[t] = seq[t]   - seq[t-1]
 y[t] = seq[t-1] - seq[t-2]
 z[t] = seq[t-2] - seq[t-3]

Using the numbers of seq above (7508, 16, 9000, 5200), x=7492, y=-8984, and z=3800.

My current method of converting codes to integers is concatenating ASCII values of each character in the code into a huge hexadecimal, i.e.

"FWR59NR0RR5NHV" -> 0x46575235394e52305252354e4856 because 0x46 is 'F', 0x57 is 'W', etc.

I have two problems with this method though:

  1. The attractor graph is composed of in-between gradient integer values that if added or subtracted to/from a code produces a code with invalid characters because ASCII includes all characters instead of only the 20 I want. If all possible codes are somehow mapped to the set {x, x+1, x+2, x+3, ... x+20^14} where x is some arbitrary starting value, then this problem can be solved.

  2. The integer values of the codes are huge (bigger than 64-bits) and encompass too many possible values that shouldn't even exist because only 20 letters are allowed instead of 256 possible values per position that are stored in the hexadecimal encoding.

In summary, what I am looking for is a function that maps all possible valid codes (with only the 20 characters shown above at each of the 14 positions) to an integer set {x, x+1, x+2, x+3, ... x+20^14} such that every valid code can be encoded into a unique integer within that set and every number within that set can be decoded back into its code.

If it is or isn't possible to perform such encoding and decoding, please let me know. Any help is appreciated.

Thank you, Vikas

  • $\begingroup$ What you want to do is to see each character as a digit of a base 20 number, and then perform base conversion to binary (base 2 or base 256 when it comes to bytes) or decimals for human consumption. So basically you take a zero-based index of a character within your alphabet, and then start multiplying with 20. So "BFH" = 0 * 20^2 + 1 * 20^1 + 2 * 20 ^ 0 = 0 + 20 + 2 = 22. Of course, you see the problem if you also allow 19 digit codes as you will get duplicate numbers if the 20 digit codes may start with a 0 valued digit. $\endgroup$ – Maarten Bodewes Jul 29 '19 at 10:10
  • $\begingroup$ I'm voting to close this question as off-topic because this encoding / decoding question is not about cryptography. $\endgroup$ – Maarten Bodewes Jul 29 '19 at 10:12
  • $\begingroup$ @Maarten Bodewes thank you for the reply, but I have one more question: how would I decode 22 back to "BFH?" $\endgroup$ – Vikas M Jul 29 '19 at 12:10
  • $\begingroup$ The easiest way to decode from "BFH" to the number 22 is to first take the index of 'B', then multiply with 20, take the next one 'F' and add it, multiply with 20 etc. To encode (not decode) from 22 to "BFH", first take the result modulus 20 - which is 2, that's the index of the least significant digit 'H', then divide by 20, take the modulus, giving you 'F' and repeat... This is basic base conversion, there should be plenty of examples. $\endgroup$ – Maarten Bodewes Jul 29 '19 at 14:58
  • $\begingroup$ @MaartenBodewes Ok thank you! $\endgroup$ – Vikas M Jul 29 '19 at 16:51

Browse other questions tagged or ask your own question.