用来表示n个字符可能组合的n进制数
import java.util.ArrayList; import java.util.List; /** * @author 源码下载 libre-free.com * */ public class GenerateKeys { // private String str = // "!\"#$%&‘()=~|QWERTYUIOP`{ASDFGHJKL+*}ZXCVBNM<>?_1234567890-^\\[email protected][asdfghjkl;:]zxcvbnm,./\\"; private static byte[] byary = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E }; /** * */ public GenerateKeys() { System.out.println("字符个数:" + byary.length); // char[] chars = str.toCharArray(); // for (char ch : chars) { // System.out.println(Integer.toHexString(ch).toUpperCase()); // } } public List<String> getKey(String str) { List<String> res = new ArrayList<String>(); for (byte by : byary) { StringBuffer buf = new StringBuffer(); buf.append(str); buf.append(String.valueOf((char) (0x0000 | by))); res.add(buf.toString()); // System.out.println(buf.toString()); } return res; } public static char getChar(int index) { byte by = byary[index]; return (char) (0x0000 | by); } public static int getIndex(char ch) { byte by = (byte) (0xFF & ch); int index = -1; for (int i = 0; i < byary.length; i++) { if (byary[i] == by) { index = i; break; } } return index; } public static int getLength() { return byary.length; } /** * @param args */ public static void main(String[] args) { GenerateKeys gk = new GenerateKeys(); gk.getKey("test"); } }
import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; import java.util.ArrayList; /** * @author 源码下载 libre-free.com * */ public class MyBigDecimal extends BigDecimal { /** * @param in */ public MyBigDecimal(char[] in) { super(in); } /** * @param val */ public MyBigDecimal(String val) { super(val); } /** * @param val */ public MyBigDecimal(double val) { super(val); } /** * @param val */ public MyBigDecimal(BigInteger val) { super(val); } /** * @param val */ public MyBigDecimal(int val) { super(val); } /** * @param val */ public MyBigDecimal(long val) { super(val); } /** * @param in * @param mc */ public MyBigDecimal(char[] in, MathContext mc) { super(in, mc); } /** * @param val * @param mc */ public MyBigDecimal(String val, MathContext mc) { super(val, mc); } /** * @param val * @param mc */ public MyBigDecimal(double val, MathContext mc) { super(val, mc); } /** * @param val * @param mc */ public MyBigDecimal(BigInteger val, MathContext mc) { super(val, mc); } /** * @param unscaledVal * @param scale */ public MyBigDecimal(BigInteger unscaledVal, int scale) { super(unscaledVal, scale); } /** * @param val * @param mc */ public MyBigDecimal(int val, MathContext mc) { super(val, mc); } /** * @param val * @param mc */ public MyBigDecimal(long val, MathContext mc) { super(val, mc); } /** * @param in * @param offset * @param len */ public MyBigDecimal(char[] in, int offset, int len) { super(in, offset, len); } /** * @param unscaledVal * @param scale * @param mc */ public MyBigDecimal(BigInteger unscaledVal, int scale, MathContext mc) { super(unscaledVal, scale, mc); } /** * @param in * @param offset * @param len * @param mc */ public MyBigDecimal(char[] in, int offset, int len, MathContext mc) { super(in, offset, len, mc); } /** * * @param str * @param numSys * @return */ public static MyBigDecimal valueOf(String str, int numSys) { char[] ary = str.toCharArray(); BigDecimal multiplier = new BigDecimal(numSys); BigDecimal product = new MyBigDecimal(0); for (int i = 0; i < ary.length; i++) { int val = GenerateKeys.getIndex(ary[i]); product = product.multiply(multiplier); product = product.add(new BigDecimal(val)); } System.out.println("val:" + product.toString()); return new MyBigDecimal(product.toString()); } /** * * @param numSys * @return */ public String toMultiNumSysString(int numSys) { MyBigDecimal divisor = new MyBigDecimal(numSys); // 除数 BigDecimal dividend = this; // 被除数 BigDecimal multiplier = dividend; boolean bFlag = false; ArrayList<BigDecimal> result = new ArrayList<BigDecimal>(); do { dividend = multiplier; multiplier = dividend.divide(divisor, BigDecimal.ROUND_DOWN); // 乘数 BigDecimal product = divisor.multiply(multiplier); // 积 BigDecimal remainder = dividend.subtract(product); // 余数 if ((bFlag == false && remainder.compareTo(BigDecimal.ZERO) != 0) || bFlag) { result.add(0, remainder); } bFlag = true; } while (multiplier.compareTo(BigDecimal.ZERO) > 0); StringBuffer buf = new StringBuffer(); for (BigDecimal d : result) { System.out.print(d.intValue() + ","); buf.append(GenerateKeys.getChar(d.intValue())); } System.out.println(); return buf.toString(); } /** * @param args */ public static void main(String[] args) { String str = "144"; MyBigDecimal a = new MyBigDecimal(str); // 十进制 String s = a.toMultiNumSysString(95); System.out.println(str + "(95)=" + s); MyBigDecimal.valueOf(s, 95); } /** * @see java.math.BigDecimal#add(java.math.BigDecimal) */ @Override public MyBigDecimal add(BigDecimal augend) { BigDecimal sum = super.add(augend); String str = sum.toString(); return new MyBigDecimal(str); } }
时间: 2024-10-11 12:55:51