进制间的相互转换

思路一:将数据转换为十进制,然后转换到对应进制。


核心代码:

	public static StringBuilder convert(String srcValue,int srcHex,int tarHex)
	{
		int num =0;
		int t = 0,i=0;
		char[] ch = srcValue.toCharArray();
		for(;i<ch.length;i++)
		{
			if(ch[i]>='0' && ch[i]<='9')
			{
				t = ch[i] - '0';
			}
			else if(ch[i] >= 'A' && ch[i] <= 'F')
			{
				t = ch[i] - 'A' + 10;
			}

			num = num * srcHex + t;
		}

		i = 0;
		StringBuilder sb = new StringBuilder(); 
		while(num != 0)
		{
			t = num % tarHex;
			char chTemp;
			if(t>=0 && t<=9)
			{
				chTemp = (char)(t + '0');
			}
			else
			{
				chTemp = (char)(t - 10 + 'A');
			}
			sb.append(chTemp);
			num /= tarHex;
		}
		return sb;
	}

测试实例:

import java.util.*;
class ConvertDemo 
{
	public static void main(String[] args) 
	{
		while(true)
		{
			System.out.println("请选择源数据的进制类型(2,8,10,16):");
			int srcHex = inputHex();
			System.out.println("请输入数据:");
			Scanner sc = new Scanner(System.in);
			String srcValue = sc.next();
			System.out.println("请选择转换的进制(2,8,10,16):");
			int tarHex = inputHex();
			StringBuilder sbResult = convert(srcValue,srcHex,tarHex);
			if(sbResult != null)
				System.out.println(srcHex + "进制数:" + srcValue + "转换为" + tarHex + "进制,结果:"+ sbResult.reverse());
			else
				System.out.println("转换失败。");
			System.out.println();
		}
	}

	public static int inputHex()
	{
		Scanner sc = new Scanner(System.in);
		while(true)
		{
			int k = sc.nextInt();
			if(!(k == 2 || k == 8 || k == 10 || k==16))
			{
				System.out.println("进制输入不正确,是否继续(y/n)?");
				String tip = sc.next();
				if("n".equals(tip) || "N".equals(tip))
				{
					return -1;
				}
			}
			else
			{
				return k;
			}
		}
	}
	public static StringBuilder convert(String srcValue,int srcHex,int tarHex)
	{
		int num =0;
		int t = 0,i=0;
		char[] ch = srcValue.toCharArray();
		for(;i<ch.length;i++)
		{
			if(ch[i]>='0' && ch[i]<='9')
			{
				t = ch[i] - '0';
			}
			else if(ch[i] >= 'A' && ch[i] <= 'F')
			{
				t = ch[i] - 'A' + 10;
			}

			num = num * srcHex + t;
		}

		i = 0;
		StringBuilder sb = new StringBuilder(); 
		while(num != 0)
		{
			t = num % tarHex;
			char chTemp;
			if(t>=0 && t<=9)
			{
				chTemp = (char)(t + '0');
			}
			else
			{
				chTemp = (char)(t - 10 + 'A');
			}
			sb.append(chTemp);
			num /= tarHex;
		}
		return sb;
	}
}


方法二:

     //下面这三个方法分别转换成相应的进制。
        public static void toBin(int num,int x)
        {
                System.out.print("您输入的"+x+"进制数的二进制形式是:");
                trans(num,1,1);
        }
        public static void toOct(int num,int x)
        {
                System.out.print("您输入的"+x+"进制数的八进制形式是:");
                trans(num,7,3);
        }
        public static void toHex(int num,int x)
        {
                System.out.print("您输入的"+x+"进制数的十六进制形式是:");
                trans(num,15,4);
        }
        
        

        //trans():上边三个进制转换函数调用这个方法就可以完成进制的转换。
        public static void trans(int num, int base, int offset)
        {
                if(num == 0)
                {
                        System.out.println(0);
                        return;
                }

                char[] chs = {'0','1','2','3',
                                        '4','5','6','7',
                                        '8','9','A','B',
                                        'C','D','E','F'};

                char[] arr = new char[32];

                int pos = arr.length;

                while(num != 0)
                {
                        int temp = num & base;
                        arr[--pos] = chs[temp];
                        num = num >>> offset;
                }

                for(int i = pos; i < arr.length; i++)
                {
                        System.out.print(arr[i]);
                }
                System.out.println();
                System.out.println();
        }


以上都只进行了整型的转换,浮点型没有。

posted @ 2015-01-19 16:28  静以养身 俭以养德  阅读(127)  评论(0编辑  收藏  举报