Python base 36 encoding

前端 未结 9 950
春和景丽
春和景丽 2020-11-29 03:21

How can I encode an integer with base 36 in Python and then decode it again?

9条回答
  •  春和景丽
    2020-11-29 03:56

    terrible answer, but was just playing around with this an thought i'd share.

    import string, math
    
    int2base = lambda a, b: ''.join(
        [(string.digits +
          string.ascii_lowercase +
          string.ascii_uppercase)[(a // b ** i) % b]
         for i in range(int(math.log(a, b)), -1, -1)]
    )
    
    num = 1412823931503067241
    test = int2base(num, 36)
    test2 = int(test, 36)
    print test2 == num
    

提交回复
热议问题