16进制以下的进制转换

对逐个数进行处理~~

package Chapter02;

import java.util.Scanner;
public class TextZhuanhuan {  //定义一个进制转换的类
	public static int ZhuanToTen(int a, String str) { //其它进制转换为十进制
		double d = 0;    //声明转换后的数值
		String subString;   //根据字符串的长度循环获得单个元素
		for(int i=0; i<str.length();i++) {
			subString = str.substring(i, i+1);
			if(a == 16){
				subString = sixteenToNumber(subString);
			}
			d += Integer.parseInt(subString) * Math.pow(a, str.length()-i-1);
		}
		return (int)d;
	}
	
	public static String TenToNumber(int a, String str) {
		int current = Integer.parseInt(str);
		String opResult = "";
		if(a == 16){
			while(current >= a){
				opResult += sixteenNumberToChar(current % a);
				current /= a;
			}
			if(current != 0)
				opResult += sixteenNumberToChar(current);
		}else{
			while(current >= a) {
				opResult += sixteenNumberToChar(current % a);
				current /= a;
			}
			if(current != 0)
				opResult += current;
		}
		String riResult = "";
		for(int i = opResult.length()-1;i>=0;i--){
			riResult = riResult + opResult.substring(i, i+1);
		}
		return riResult;
	}
	
	public static String sixteenToNumber(String s) {
		String num = "";
		if(s.equals("A") || s.equals("a"))
			num = "10";
		else if(s.equals("B") || s.equals("b"))
			num = "11";
		else if(s.equals("C") || s.equals("c"))
			num = "12";
		else if(s.equals("D") || s.equals("d"))
			num = "13";
		else if(s.equals("E") || s.equals("e"))
			num = "14";
		else if(s.equals("F") || s.equals("f"))
			num = "15";
		else num = s;
		return num;
	}
	
	public static String sixteenNumberToChar(int num) {
		String c="";
		if(num == 10)
			c = "A";
		else if(num == 11)
			c = "B";
		else if(num == 12)
			c = "C";
		else if(num == 13)
			c = "D";
		else if(num == 14)
			c = "E";
		else if(num == 15)
			c = "F";
		else 
			c = String.valueOf(num);
		return c;
	}
	
	public static void main(String[] args) {
		String number; 					// 要转换的数
		int a, b;							// a表示转换前的进制,b表示转换后的进制
		String result = "";					// 经过数制转换后的结果
		String stop = "";
		Scanner read = new Scanner(System.in);//得到用户输入的值
		do{
			System.out.println("输入三个整数:待转换的数据   要转换之前的进制  要转换后的进制");
			number = read.next();
			a = read.nextInt();
			b = read.nextInt();
			stop = "Quit";
		}while(stop != "Quit");
		try{
			if(a != 10){
				String temp = String.valueOf(ZhuanToTen(a, number)); 	// 转换成十进制的数
				result = String.valueOf(TenToNumber(b, temp)); 		// 十进制转换成其它进制
			} else {
				result = String.valueOf(TenToNumber(b, number)); 		// 十进制转换成其它进制
			}
			System.out.println(a + "进制的数:" + number + ",转换成" + b + "进制的数为:"
					+ result);
		} catch (Exception e) {
			System.out.print("转换失败,请正确输入!");
			System.exit(-1);
		}
	}

}

1


posted on 2012-07-20 19:23  java课程设计例子  阅读(197)  评论(0编辑  收藏  举报