Convert int to ASCII and back in Python


Question

I'm working on making a URL shortener for my site, and my current plan (I'm open to suggestions) is to use a node ID to generate the shortened URL. So, in theory, node 26 might be short.com/z, node 1 might be short.com/a, node 52 might be short.com/Z, and node 104 might be short.com/ZZ. When a user goes to that URL, I need to reverse the process (obviously).

I can think of some kludgy ways to go about this, but I'm guessing there are better ones. Any suggestions?

1
120
1/26/2016 5:42:41 PM

ASCII to int:

ord('a')

gives 97

And back to a string:

  • in Python2: str(unichr(97))
  • in Python3: str(chr(97))

gives 'a'

209
11/26/2018 11:33:17 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon