首页 > 其他 > 详细

进制转换模板

时间:2014-03-12 18:57:38      阅读:421      评论:0      收藏:0      [点我收藏+]

下面的例子是16进制串转换成3进制串输出。

其他进制之间的相互转换,只需改动一下下面代码中有注释的地方。


import java.util.Scanner;

public class jinzhizhuanhuan {
	
		private static int getRealValue(char x)
		{
			if(x>=‘0‘ && x<=‘9‘) return x-‘0‘;
			if(x>=‘a‘ && x<=‘f‘) return x-‘a‘+10;
			if(x>=‘A‘ && x<=‘F‘) return x-‘A‘+10;
			return 0;
		}

	public static String jin_zhi_16_3(String x)	{
		int n = 0; // 累加真值
		for(int i=0; i<x.length(); i++)
		{
			n = 16*n + getRealValue(x.charAt(i));  //
		}
		String t = "";
		for(;;)
		{
			if(n==0) break;
			t = (n % 3) + t;  //
			n = n / 3;  //
		}
		return t;
	}
	
	public static void main(String[] args) {
		String x = "";
		Scanner input = new Scanner(System.in);
		while(input.hasNext()){
			x = input.next();
			String t = jin_zhi_16_3(x);
			System.out.println(t);
		}
	}
}


进制转换模板,布布扣,bubuko.com

进制转换模板

原文:http://blog.csdn.net/u010127250/article/details/21103901

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!