Java之查表法实现进制转换

/*
   层层深入,步步为赢
*/
class BinaryTest2 
{
	//通过查表法,实现十进制转变成十六进制
	public static void main(String[] args) 
	{
		toHex_1(60);
		toHex_2(60);
		toHex_3(60);
		toHex_4(60);
		toHex_5(60);
		toHex_6(60);
		toHex_7(60);
		toHex_8(60);
		toHex_9(60);
	}
	//查表法;步骤一
	public static void toHex_1(int num)
	{
		char[] ch = {'0','1','2','3'
					,'4','5','6','7'
					,'8','9','A','B'
					,'C','D','E','F'};
		for (int x = 0;x<8 ;x++ )
	    {
			int temp = num & 15;
			//这里是按照角标取得的值,所以这里不需要判断
			System.out.print(ch[temp]);
			num = num >>> 4;
		}
		System.out.println();
	}
	//查表法:步骤二
	public static void toHex_2(int num)
	{
		char[] ch = {'0','1','2','3'
					,'4','5','6','7'
					,'8','9','A','B'
					,'C','D','E','F'};
		//定义一个临时容器
		char[] cha = new char[8];
		for (int x = 0;x<8 ;x++ )
	    {
			/*
			步骤:
			1,首先将数字1111“&”一下
			2,将临时数据存储到临时的容器中
			3,然后在将数字右移
			*/
			int temp = num & 15;
			//System.out.print(ch[temp]);
			cha[x] = ch[temp];//将每一个最低的四位数字,计算出来,存到临时的容器当中
			num = num >>> 4;
			System.out.print(cha[x]);
		}
		System.out.println();
	}
	//查表法:步骤三
	public static void toHex_3(int num)
	{
		char[] ch = {'0','1','2','3'
					,'4','5','6','7'
					,'8','9','A','B'
					,'C','D','E','F'};
		//定义一个临时容器
		char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
		for (int x = 0;x<8;x++)
	    {
			int temp = num & 15;
			//System.out.print(ch[temp]);
			cha[x] = ch[temp];//往里面传输数据的时候,指针是在移动的
			num = num >>> 4;
		}
		//遍历输出临时存储容器中的所存储的东西
		//将数据从最后位输出
		for (int x = cha.length-1;x >= 0 ;x-- )
		{
			System.out.print(cha[x]+" ");
		}
		System.out.println();
	}
	//查表法:步骤四
	public static void toHex_4(int num)
	{
		char[] ch = {'0','1','2','3'
					,'4','5','6','7'
					,'8','9','A','B'
					,'C','D','E','F'};
		//定义一个临时容器
		char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
		int poster = 0;
		while (num != 0)
	    {
			int temp = num & 15;
			//System.out.print(ch[temp]);
			cha[poster++] = ch[temp];//往里面传输数据的时候,指针是在移动的
			num = num >>> 4;
		}
		//遍历输出临时存储容器中的所存储的东西
		//将数据从最后位输出
		for (int x = cha.length-1;x >= 0 ;x-- )
		{
			System.out.print(cha[x]+",");
		}
		System.out.println();
	}
	//查表法:步骤五
	public static void toHex_5(int num)
	{
		char[] ch = {'0','1','2','3'
					,'4','5','6','7'
					,'8','9','A','B'
					,'C','D','E','F'};
		//定义一个临时容器
		char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
		int poster = 0;
		while (num != 0)
	    {
			int temp = num & 15;
			//System.out.print(ch[temp]);
			cha[poster++] = ch[temp];//往里面传输数据的时候,指针是在移动的
			num = num >>> 4;
		}
		//遍历输出临时存储容器中的所存储的东西
		//将数据从最后位输出
		for (int x = poster;x >= 0 ;x-- )
		{
			System.out.print(cha[x]+",");
		}
		System.out.println();
	}
	//查表法:步骤六
	public static void toHex_6(int num)
	{
		char[] ch = {'0','1','2','3'
					,'4','5','6','7'
					,'8','9','A','B'
					,'C','D','E','F'};
		//定义一个临时容器
		char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
		int poster = 0;
		while (num != 0)
	    {
			int temp = num & 15;
			//System.out.print(ch[temp]);
			cha[poster++] = ch[temp];//往里面传输数据的时候,指针是在移动的
			num = num >>> 4;
		}
		//遍历输出临时存储容器中的所存储的东西
		//将数据从最后位输出
		for (int x = poster-1;x >= 0 ;x-- )
		{
			System.out.print(cha[x]+",");
		}
		    System.out.println();
	}
		//查表法:步骤七
	public static void toHex_7(int num)
	{
		char[] ch = {'0','1','2','3'
					,'4','5','6','7'
					,'8','9','A','B'
					,'C','D','E','F'};
		//定义一个临时容器
		char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
		int poster = cha.length-1;
		while (num != 0)
	    {
			int temp = num & 15;
			//System.out.print(ch[temp]);
			cha[poster--] = ch[temp];//往里面传输数据的时候,指针是在移动的
			num = num >>> 4;
		}
		//遍历输出临时存储容器中的所存储的东西
		//将数据从最后位输出
		for (int x = poster;x <cha.length ;x++)
		{
			System.out.print(cha[x]+",");
		}
		    System.out.println();
	}
		//查表法:步骤八
	public static void toHex_8(int num)
	{
		char[] ch = {'0','1','2','3'
					,'4','5','6','7'
					,'8','9','A','B'
					,'C','D','E','F'};
		//定义一个临时容器
		char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
		int poster = cha.length-1;
		while (num != 0)
	    {
			int temp = num & 15;
			//System.out.print(ch[temp]);
			cha[poster--] = ch[temp];//往里面传输数据的时候,指针是在移动的
			num = num >>> 4;
		}
		//遍历输出临时存储容器中的所存储的东西
		//将数据从最后位输出
		for (int x = poster+1;x <cha.length ;x++)
		{
			System.out.print(cha[x]+",");
		}
		    System.out.println();
	}
		//查表法:步骤九(最终版)
	public static void toHex_9(int num)
	{
		char[] ch = {'0','1','2','3'
					,'4','5','6','7'
					,'8','9','A','B'
					,'C','D','E','F'};
		//定义一个临时容器
		char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
		int poster = cha.length;
		while (num != 0)
	    {
			//取出有效位
			int temp = num & 15;
			//System.out.print(ch[temp]);
			cha[--poster] = ch[temp];//往里面传输数据的时候,指针是在移动的
			num = num >>> 4;
		}
		//遍历输出临时存储容器中的所存储的东西
		//将数据从最后位输出
		for (int x = poster;x <cha.length ;x++)
		{
			System.out.print(cha[x]+",");
		}
		    System.out.println();
	}
}


   在这里我给大家演示的是实现十进制转变成十六进制步骤,其他的进制转换都一样,不多说了,给大家演示一下

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值