java中四种进制的转换

package com.chapter02;

import java.util.Scanner;

public class SysConvert {
	/**
	 * other system convert into ten system
	 * 
	 * @param a
	 * @param str
	 * @return  d
	 */
	public static int otherToTen(int a, String str) {
		double d = 0; // number after convert
		String subString; // loop acquire single element on the basis of the
							// length of the string
		for (int i = 0; i < str.length(); i++) {
			subString = str.substring(i, i + 1); // loop cut the string
			if (a == 16) {
				subString = sixteenToNumber(subString); // convert the char to
														// number
			}
			d += Integer.parseInt(subString)
					* Math.pow(a, str.length() - i - 1);
		}
		return (int) d;
	}

	/**
	 * convert ten system to other system
	 * 
	 * @param a
	 * @param str
	 * @return 
	 */
	public static String tenToNumber(int a, String str) {
		int current = Integer.parseInt(str);
		String opResult = "";
		// Judge whether the number after convert is sixteen system
		if (a == 16)
		// Judge whether the current number is bigger than sixteen.
		// If it is,when meet sixteen,carry a number 1
		{
			while (current >= a) {
				opResult += sixteenNumberToChar(current % a);
				current /= a;
			}
			if (current != 0)
				opResult += sixteenNumberToChar(current);
		} else {
			// Judge whether the current number is bigger than the system after
			// convert
			while (current >= a) {
				opResult += current % a;
				current /= a;
			}
			if (current != 0)
				opResult += current;
		}
		String riResult = ""; // binary system string of inverted order
		// loop print out by binary system
		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) {
		// sixteenNumber convert to char
		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;
	}

	/**
	 * main,the entry of this program
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		String number; // the number about to convert
		int a, b; // a means the number before convert
		// b means the number after convert
		String result;	//the result after convert
		String stop;	
		Scanner read = new Scanner(System.in);
		do{
			System.out.println("Input three interger numbers:the number before convert,pre system,later system");
			number = read.next();
			a = read.nextInt();
			b = read.nextInt();
			stop = "Quit";
		}while(stop != "Quit");
		
		try{
			if(a != 10)	//Judge whether the number before convert is ten system
			{
				String temp = String.valueOf(otherToTen(a,number));	//the number convert to ten system
				result = String.valueOf(tenToNumber(b, temp));	//ten system convert to other system
			}
			else
			{
			result = String.valueOf(tenToNumber(b,number));	//ten system convert to other system
			}
			System.out.println(a + " system's number " + number + " convert to " + b + " system,\nthe result is " + result);
		}catch(Exception e)
		{
			System.out.println("Failed to convert,please input correctly!");
			System.exit(-1);
		}
	}
}


转载于:https://my.oschina.net/clear/blog/61919

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值