本文介绍了使用Java中的固定字母集编码/解码长字符串到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 给定任意字母集String range = "0123456789abcdefghijklmnopABCD#"; 我正在寻找2个方法来编码/解码从long StringI am looking for 2 methods to encode/decode from long <-> StringString s = encode( range, l );和long l = decode( range, s );因此 decode(range,encode(range,123456789L))== 123456789LSo decode(range, encode(range, 123456789L)) == 123456789L如果范围是0123456789,这是通常的编码方式。And if range is "0123456789" thats the usual way of encoding.推荐答案以下代码可以满足您的需要:The following code does what you need:static long decode(String s, String symbols) { final int B = symbols.length(); long num = 0; for (char ch : s.toCharArray()) { num *= B; num += symbols.indexOf(ch); } return num;}static String encode(long num, String symbols) { final int B = symbols.length(); StringBuilder sb = new StringBuilder(); while (num != 0) { sb.append(symbols.charAt((int) (num % B))); num /= B; } return sb.reverse().toString();}public static void main(String[] args) { String range = "0123456789abcdefghijklmnopABCD#"; System.out.println(decode(encode(123456789L, range), range)); // prints "123456789" System.out.println(encode(255L, "0123456789ABCDEF")); // prints "FF" System.out.println(decode("100", "01234567")); // prints "64"} 用不同字母表长度替换密码substitution cypher with different alphabet length 这篇关于使用Java中的固定字母集编码/解码长字符串到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 17:23