1

I'd like to encode certain information, such as date and time of build and the SVN revision, into a reversible alphanumeric string so I can easily identify a build (besides maintaining a table of known builds).

I have no idea of how to go about this. Can you point me to related algorithms, commands, etc that will help me with this?

1 Answers1

1

If you don’t need it to be too too short, use Base64 encoding.

$ echo $(date +%s)-r3749 | base64
MTM1ODg4MzA3MS1yMzc0OQo=
$ echo MTM1ODg4MzA3MS1yMzc0OQo= | base64 --decode
1358883071-r3749

Here I used an Epoch time since it’s fewer characters than a full date. Since it’s a number, and the SVN revision is a number too, you could also encode them using a different base. Using this Base 62 encoding function, you could reversibly encode the (date, revision) pair 1358883071-r3749 as 1tXJyT,Yt

But Base64 is probably short enough, and it’s super easy.

Community
  • 1
  • 1
andrewdotn
  • 32,721
  • 10
  • 101
  • 130
  • Thanks for your answer; it's a great start! Upon further thought, all the information I want to encode can be represented by numbers alone. ddmmyyhhss+rev which comes upto 14 digits which can be encoded in 7 bytes. Is this the lowest possible though? – user1988953 Jan 22 '13 at 19:43
  • If you just want ddmmyyhhmm+rev, and suppose you want to support 30 different year values and revs up to 50,000—then there are 31 days * 12 months * 30 years * 24 hours * 60 minutes * 50000 revs = 803520000000 possible values to store. You would need log(803520000000)/log(2)= 39.5 bits to store that, meaning that if you were to use bit-packing you would only need 5 bytes. But that requires a pretty complicated implementation and isn’t even using alphanumeric characters anymore :/ With log(62) for alphanumeric and bitpacking and max rev of 3300 it could be only 6 bytes … – andrewdotn Jan 22 '13 at 20:24