Python base 36 encoding

前端 未结 9 999
春和景丽
春和景丽 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:46

    You can use numpy's base_repr(...) for this.

    import numpy as np
    
    num = 2017
    
    num = np.base_repr(num, 36)
    print(num)  # 1K1
    
    num = int(num, 36)
    print(num)  # 2017
    

    Here is some information about numpy, int(x, base=10), and np.base_repr(number, base=2, padding=0).

    (This answer was originally submitted as an edit to @christopher-beland's answer, but was rejected in favor of its own answer.)

提交回复
热议问题