00 第0瓶
1 1
0 2
1 3
0 4
1 5
0 6
1 7
public class Base { /** * 將數轉為任意進位 * @param num * @param base * @return */ public String baseString(int num,int base){ if(base > 16){ throw new RuntimeException("進位數超出範圍,base<=16"); } StringBuffer str = new StringBuffer(""); String digths = "0123456789ABCDEF"; Stack<Character> s = new Stack<Character>(); while(num != 0){ s.push(digths.charAt(num%base)); num/=base; } while(!s.isEmpty()){ str.append(s.pop()); } return str.toString(); } /** * 16進位內任意進位轉換 * @param num * @param srcBase * @param destBase * @return */ public String baseNum(String num,int srcBase,int destBase){ if(srcBase == destBase){ return num; } String digths = "0123456789ABCDEF"; char[] chars = num.toCharArray(); int len = chars.length; if(destBase != 10){//目標進位不是十進位 先轉化為十進位 num = baseNum(num,srcBase,10); }else{ int n = 0; for(int i = len - 1; i >=0; i--){ n+=digths.indexOf(chars[i])*Math.pow(srcBase, len - i - 1); } return n + ""; } return baseString(Integer.valueOf(num),destBase); } }
轉載請標明出處:http://blog.csdn.net/u012027907