Base36编码一个字符串?

我一直在网上寻找,但无法找到解决方案。 在Python,Ruby或Java中,我如何基于36编码以下字符串:nOrG9Eh0uyeilM8Nnu5pTywj3935kW + 5 =

ruby


以36为基础:

s.unpack('H*')[0].to_i(16).to_s 36 

从36号基地:

 [s36.to_i(36).to_s(16)].pack 'H*' 

看起来维基百科有一篇关于如何在python中执行此操作的文章: http : //en.wikipedia.org/wiki/Base_36

我刚做完了:

  static String encodeRootId(final String value) { try { final BigInteger bigInteger = new BigInteger(value.getBytes("UTF-8")); final String encoded = bigInteger.toString(Character.MAX_RADIX); //must encode the sign as well if (bigInteger.signum() < 0) { return 'n' + encoded.substring(1); } else { return 'p' + encoded; } } catch (UnsupportedEncodingException e) { throw new RuntimeException("impossible"); } } 

将字符串bytes []转换为大型int的技巧带来了必须手动编码可能性的缺点 - 当然不是很好但是快速的解决方案。

另外,在我的用例中,我不必解码,性能也不是问题。

这可以看作是另一个问题的重复… Python基础36编码 。

基本上,维基百科上有这个例子:

 def base36encode(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'): """Convert positive integer to a base36 string.""" if not isinstance(number, (int, long)): raise TypeError('number must be an integer') # Special case for zero if number == 0: return alphabet[0] base36 = '' sign = '' if number < 0: sign = '-' number = - number while number != 0: number, i = divmod(number, len(alphabet)) base36 = alphabet[i] + base36 return sign + base36 def base36decode(number): return int(number, 36) print base36encode(1412823931503067241) print base36decode('AQF8AA0006EH') 

维基百科页面: http : //en.wikipedia.org/wiki/Base_36