title: Encoding a 128-bit integer in Python?
body: <p>Inspired by the "encoding scheme" of the answer to <a href="https://stackoverflow.com/questions/742013/how-to-code-a-url-shortener">this question</a>, I implemented my own encoding algorithm in Python. </p>
  
  <p>Here is what it looks like:</p>
  
  <pre><code>import random
  from math import pow
  from string import ascii_letters, digits
  
  # RFC 2396 unreserved URI characters
  unreserved = '-_.!~*\'()'
  characters = ascii_letters + digits + unreserved
  size = len(characters)
  seq = range(0,size)
  
  # Seed random generator with same randomly generated number
  random.seed(914576904)
  random.shuffle(seq)
  
  dictionary = dict(zip(seq, characters))
  reverse_dictionary = dict((v,k) for k,v in dictionary.iteritems())
  
  def encode(n):
      d = []
      n = n
      while n &gt; 0:
          qr = divmod(n, size)
          n = qr[0]
          d.append(qr[1])
      chars = ''
      for i in d:
          chars += dictionary[i]
      return chars
  
  def decode(str):
      d = []
      for c in str:
          d.append(reverse_dictionary[c])
      value = 0
      for i in range(0, len(d)):
          value += d[i] * pow(size, i)
      return value
  </code></pre>
  
  <p>The issue I'm running into is encoding and decoding very large integers. For example, this is how a large number is currently encoded and decoded:</p>
  
  <pre><code>s = encode(88291326719355847026813766449910520462)
  # print s -&gt; "3_r(AUqqMvPRkf~JXaWj8"
  i = decode(s)
  # print i -&gt; "8.82913267194e+37"
  # print long(i) -&gt; "88291326719355843047833376688611262464"
  </code></pre>
  
  <p>The highest 16 places match up perfectly, but after those the number deviates from its original.</p>
  
  <p>I assume this is a problem with the precision of extremely large integers when dividing in Python. Is there any way to circumvent this problem? Or is there another issue that I'm not aware of?</p>