package p5.com.byd.mes.dev.tool.baseToChar; /** * 任意进制转换器 * @author solar */ public class Decimal { String imalss = ""; String[] imal = new String[10]; int flag = 0; public Decimal(String imals) { String[] temp = imals.split(""); String[] imalTemp = new String[temp.length - 1]; for (int i = 0; i < imalTemp.length; i++) { imalTemp[i] = temp[i + 1].toUpperCase(); } imalss = imals.toUpperCase(); imal = imalTemp; flag = imal.length; } public long toDec(String in) throws Exception { long temp = 0; in = in.toUpperCase(); String[] ins = in.split(""); for (int i = 1; i < ins.length; i++) { long ff = imalss.indexOf(ins[i]); if (ff == -1) { throw new Exception("非法字符串"); } temp = temp + solar(ins.length - i) * ff; } return temp; } public String parseDec(long in) throws Exception { StringBuffer sbTemp = new StringBuffer(); long i = in % flag; while (in != 0) { sbTemp.append(imal[(int) i]); in = in / flag; i = in % flag; } sbTemp.reverse(); return new String(sbTemp); } private long solar(int length) { long temp = 1; while (length > 1) { temp = temp * flag; length--; } return temp; } /*************************************************************************** * 单元测试 * @param args the command line arguments **************************************************************************/ public static void main(String[] args) throws Exception { Decimal dec = new Decimal("23456789BCDFGHJKLMNPQRSTVWXZ");//构造一个指定的进制对象。 //System.out.println(dec.toDec("23420")); System.out.println(dec.parseDec(892)); } }
private static final Log log = LogFactory.getLog(Base28Impl.class); private static Decimal dec = new Decimal("23456789BCDFGHJKLMNPQRSTVWXZ");//构造自定义28进制的对象 @Override public String getDesc() { return "二十八进制"; }
@Override public String get(long cursor, NextNumberBO nextNumberBO) { String data = null; try { data = dec.parseDec(cursor); } catch (Exception e) { log.debug(e.getMessage()); throw new RuntimeException("二十八进制转换失败!"); } long sequenceLength = 0L; try { sequenceLength = dec.toDec(nextNumberBO.getSequenceLength()); } catch (Exception e) { log.debug(e.getMessage()); } //产生二十八进制的流水号。 return StringUtils.leftPad(data, (int)sequenceLength, "2"); } @Override public boolean isEngenderNextNumber(NextNumberBO nextNumberBO) { long maxSequence = 0L; long currentSequence = 0L; try { maxSequence = dec.toDec(nextNumberBO.getMaxSequence()); currentSequence = dec.toDec(nextNumberBO.getCurrentSequence()); } catch (Exception e) { log.debug(e.getMessage()); } long qty = maxSequence - nextNumberBO.getDistributeNum().longValue(); long newQty = currentSequence + nextNumberBO.getIncr().longValue(); //已分配数量少于最大序列 且 可分配数量要大于等于增量 且 最大序列要大于 (当前序列+增量)的和 boolean falg = nextNumberBO.getDistributeNum().longValue() < maxSequence && qty >= nextNumberBO.getIncr().longValue() && maxSequence > newQty; return falg; } @Override public boolean validate(NextNumberBO nextNumberBO) { if(!validateSequence(nextNumberBO.getSequenceLength().toString())) throw new RuntimeException("验证序列长度失败,请填写正确的数据进行操作!"); if(!validateSequence(nextNumberBO.getMaxSequence().toString())) throw new RuntimeException("验证最大序列失败,请填写正确的数据进行操作!"); if(!validateSequence(nextNumberBO.getMinSequence().toString())) throw new RuntimeException("验证最小序列失败,请填写正确的数据进行操作!"); if(!validateSequence(nextNumberBO.getCurrentSequence().toString())) throw new RuntimeException("验证当前序列失败,请填写正确的数据进行操作!"); long maxSequence = 0L; long minSequence = 0L; long currentSequence = 0L; try { maxSequence = dec.toDec(nextNumberBO.getMaxSequence()); minSequence = dec.toDec(nextNumberBO.getMinSequence()); currentSequence = dec.toDec(nextNumberBO.getCurrentSequence()); } catch (Exception e) { log.debug(e.getMessage()); } if(maxSequence < minSequence) throw new RuntimeException("最大序列必须大于最小序列!"); if(maxSequence < currentSequence) throw new RuntimeException("最大序列必须大于当前序列!"); long distributeUnitNum = nextNumberBO.getDistributeUnitNum().longValue(); if(distributeUnitNum > maxSequence) throw new RuntimeException("预加载分配单元数不能大于最大序列"); long distributeNum = nextNumberBO.getDistributeNum().longValue(); if(distributeNum > maxSequence) throw new RuntimeException("已分配数不能大于最大序列"); return true; } /** * 验证是否是正确的二十八进制数值 * @param sequence * @return */ private boolean validateSequence(String sequence){ Pattern pattern = Pattern.compile("[0-9B-Z&&[^EYUIO]]+");//二十八进制的正则表达式 return pattern.matcher(sequence).matches(); } /** * 28进制的日期格式转换 * @param pattern * @return */ @Override public String toDate(String pattern){ SimpleDateFormat format = new SimpleDateFormat(pattern); String result = format.format(new Date()); StringBuffer buffer = new StringBuffer(); Date date = null; try { date = (Date)format.parseObject(result); } catch (ParseException e) { e.printStackTrace(); } if(pattern.indexOf("yyyy") > -1){ String year = Base28Utils.getYear(date.getYear()+1900); buffer.append(year); } if(pattern.indexOf("MM") > -1){ String month = Base28Utils.getMonth(date.getMonth()+1); buffer.append(month); } if(pattern.indexOf("dd") > -1){ String day = getResult(date.getDate()); buffer.append(day); } if(pattern.indexOf("HH") > -1){ String hours = getResult(date.getHours()); buffer.append(hours); } if(pattern.indexOf("mm") > -1){ String minutes = getResult(date.getMinutes()); buffer.append(minutes); } if(pattern.indexOf("ss") > -1){ String seconds = getResult(date.getSeconds()); buffer.append(seconds); } return buffer.toString(); } /** * * getResult:(将数值转换成相对应的进制) * @param @param data * @return String */ private String getResult(long data){ try{ return dec.parseDec(data); }catch(Exception ex){ } return null; }