1

I've got a bit of ID generation code that generates numbers. The customer would like for it to instead use letters and numbers, so that the generated IDs are shorter.

In other words, instead of the numeric IDs they want:

 0 => a
 1 => b
 26 => A
 52 => 1
 62 => aa

Or something like that. The main point is that the IDs should start with a number, and be translatable back to a number, but use characters in between to keep the ID short.

I can think of lots of different ways to go about this, but none seem simple/obvious.

Is there a simple/obvious way in Python to convert numbers in to 62-bit (a-z = 26 + A-Z = 26 + 0-9 = 10 ... 62)) character strings (and back)?

machineghost
  • 33,529
  • 30
  • 159
  • 234
  • See here: http://stackoverflow.com/questions/1119722/base-62-conversion-in-python. You can change the alphabet. – Qrtn May 09 '14 at 21:24
  • That's perfect, I don't know why I couldn't find that when I tried seraching; I'll flag this question as a close/duplicate. Thanks! – machineghost May 09 '14 at 21:25
  • Also related [convert integer to a string given numeric base in python](http://stackoverflow.com/questions/2267362/convert-integer-to-a-string-in-a-given-numeric-base-in-python) – mtadd May 09 '14 at 21:27

0 Answers0