java--十进制转化为二进制、十进制转化为十六进制、查表法十进制--十六进制、查表法十进制二进制、

对StringBuffer的使用说明:

package test;

public class test {
	    public static void main(String[] args) {
	        StringBuffer sb = new StringBuffer();//StringBuilder sb=new StringBuilder();具有同种功效
	        int len = sb.length();
	        System.out.println(len);//长度是0
	        sb.append("hello");//添加字符串进去
	        System.out.println("sb:" + sb);
	         
	        StringBuffer sb1 = new StringBuffer("world");//构造的时候,就添加字符串
	        System.out.println("sb1:" + sb1);
	 
	    }
}

控制台输出结果:
0
sb:hello
sb1:world


(一)十进制化为二进制:

package test;

public class test {
	public static void main(String[] args)
	{
		tobin(6);
	}
	//十进制转化为二进制
	public static void tobin(int num)
	{
		StringBuffer ab=new StringBuffer();
		while(num>0)
		{
			System.out.println(num%2);
			num=num/2;
		}
		System.out.println(ab.reverse());
	}
}


控制台输出结果:
0
1
1

(二)十进制转化为十六进制:

先将十进制转化为二进制,然后再将二进制转化为十六进制:比防说60转化为十六进制是3C,60的二进制是:

实际上,&与操作是做了一种单位求余的操作,且将余数的可能缩小在了不会超过单位最大值的范围之内

0000-0000    0000-0000   0000-0000  0011-1100

0000-0000    0000-0000   0000-0000  0000-1111

上面两个进行与“&“运算:

0000-0000    0000-0000   0000-0000   0000-1111


package test;

public class test {
	public static void main(String[] args)
	{
		tohex(60);
	}
	//十进制转化为十六进制
	//第一种:
	public static void tohex(int num)
	{
		StringBuffer sb=new StringBuffer();
		for(int x=0;x<6;x++)
		{
			int temp=num&15;
			if(temp>9)
			sb.append((char)(temp-10+'A'));
			else
				sb.append(temp);
			num=num>>>4;//右移四位
		}
		System.out.println(sb.reverse());
	}
	/*控制台输出结果:
	 * 00003C
	 */
	
	
	
	//第二种:
	/*public static void tohex(int num)
	{
		StringBuffer sb=new StringBuffer();
		for(int x=0;x<6;x++)
		{
			int temp=num&15;
			if(temp>9)
			System.out.println((char)(temp-10+'A'));
			else
				System.out.println(temp);
			num=num>>>4;
		}
	}
	
	C
	3
	0
	0
	0
	0
    */
}



(三)查表法十进制十六进制:

package test;

public class test {
	
	/*0 1 2 3 4 5 6 7 8 9 A   B   C   D   E   F   ==十六进制中的元素 
	 *0 1 2 3 4 5 6 7 8 9 10  11  12  13  14  15
	 * 
	 * 查表法:将所有元素临时存储起来,建立对应关系
	 * 每一次&15后的之作为索引去查建立好的表,就可以找到对应的元素
	 * 这样比-10+‘a’简单的多
	 * 这个表怎么建立呢?
	 * 可以通过数据的形式来定义
	 */
	public  static void main(String [] args)
	{
		
		tohex(60);
	}
	
	public static void tohex(int num)
	{
		char[] sh={'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.println(sh[temp]);
			num=num>>>4;
		}
	}
}


控制台输出结果:
C
3
0
0
0
0
0
0

package test;

public class test {
	
	/*0 1 2 3 4 5 6 7 8 9 A   B   C   D   E   F   ==十六进制中的元素 
	 *0 1 2 3 4 5 6 7 8 9 10  11  12  13  14  15
	 * 
	 * 查表法:将所有元素临时存储起来,建立对应关系
	 * 每一次&15后的之作为索引去查建立好的表,就可以找到对应的元素
	 * 这样比-10+‘a’简单的多
	 * 这个表怎么建立呢?
	 * 可以通过数据的形式来定义
	 */
	public  static void main(String [] args)
	{
		tohex(60);
	}
	public static void tohex(int num)
	{
		char[] sh={'0','1','2','3',
				   '4','5','6','7',
				   '8','9','A','B',
				   'C','D','E','F'};
		//定义一个临时容器
		char[] arr=new char[8];//'\u0000'
		int post=0;
		while(num!=0)
		{
			int temp=num&15;
			
			arr[post++]=sh[temp];
			num=num>>>4;
		}
		//存储数据的数组遍历
		for(int x=post-1;x>=0;x--)
		{
			System.out.print(arr[x]+",");
		}
	}
	
	//控制台输出结果:3,C,
	
	/*
	 * public static void tohex(int num)
	{
		char[] sh={'0','1','2','3',
				   '4','5','6','7',
				   '8','9','A','B',
				   'C','D','E','F'};
		//定义一个临时容器
		char[] arr=new char[8];
		for(int x=0;x<8;x++)
		{
			int temp=num&15;
			
			arr[x]=sh[temp];
			num=num>>>4;
		}
		//存储数据的数组遍历
		for(int x=arr.length-1;x>=0;x--)
		{
			System.out.print(arr[x]+",");
		}
	}
	
	//0,0,0,0,0,0,3,C,
	 */
}
package test;

public class test {
	public  static void main(String [] args)
	{
		tohex(60);
	}
	public static void tohex(int num)
	{
		char[] sh={'0','1','2','3',
				   '4','5','6','7',
				   '8','9','A','B',
				   'C','D','E','F'};
		//定义一个临时容器
		char[] arr=new char[8];//'\u0000'
		int post=arr.length;
		while(num!=0)
		{
			int temp=num&15;
			
			arr[--post]=sh[temp];
			num=num>>>4;
		}
		//存储数据的数组遍历
		System.out.println("post="+post);
		for(int x=post;x<arr.length;x++)
		{
			System.out.print(arr[x]+",");
		}
	}
}
控制台输出结果:
post=6
3,C,


(查表法十进制转化为二进制):

package test;  
  
public class test {  
	public static void main(String[] args)
	{
		fun(6);
	}
	
	public static void fun(int num)
	{
		char[] a={'0','1'};
		char[] arr=new char[32];
		int post=arr.length;
		while(num!=0)
		{
			int temp=num&1;
			arr[--post]=a[temp];
			num=num>>>1;
		}
		for(int x=post;x<arr.length;x++)
		{
			System.out.print(arr[x]);
		}
	}
}  
控制台输出结果:
110



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值