黑马程序员——代码实现进制转换

                                    

                                          ----------------------    android培训      java培训   期待与您交流!    ----------------------   
                                                          
系统提供的进制转换为Intege.toBinaryString();等等一系列的方法重载,下面是我自己写的代码转换
package com.be.four;
public class Change {
 public static void main(String[] args) {
  shi_er(60);
 }
 /*
  * 十进制转换为二进制
  */
 public static void ten_two(int a){
  StringBuffer sb=new StringBuffer();
  while(a>0){
   sb.append(a%2);
   a=a/2;
  }
  System.out.println(sb.reverse());
 }
 /*
  * 十六进制转换为二进制
  */
 public static void sixteen_two(int a){
  StringBuffer sb=new StringBuffer();
  while(a>0){
   sb.append(a%16);
   a=a/16;
  }
  System.out.println(sb.reverse());
 }
 /*
  * 十进制转换为十六进制
  */
 public static void ten_sixteen(int a){
  StringBuffer sb=new StringBuffer();
  for (int i = 0; i < 8; i++) {
   int temp=a&15;
   if(temp>9){
    sb.append((char)(temp-10+'a'));
   }else{
    sb.append(temp);
   }
   a=a>>>4;
  }
  System.out.println(sb.reverse());
 }
 /*
  * 十进制转换为十六进制
  * 查表法
  */
 public static void ten_sixteen_select(int a){
  char[] sixteen={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  StringBuffer sb=new StringBuffer();
  for (int i = 0; i < 8; i++) {
   int temp=a&15;
   sb.append(sixteen[temp]);
   a=a>>>4;
  }
  System.out.println(sb.reverse());
 }
 /*
  * 十进制转换为十六进制
  * 查表法2,不用StringBuffer,用数组
  */
 public static void ten_sixteen_select_2(int a){
  char[] sixteen={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  char[]cunchu=new char[8];
  int add=cunchu.length;
  while(a!=0){
   int temp=a&15;
   cunchu[--add]=sixteen[temp];
   a=a>>>4;
  }
  System.out.println(add);
  for (int i = add; i <cunchu.length; i++) {
   System.out.print(cunchu[i]+",");
  }
  
 }
 /*
  * 十进制转换为二进制
  * 查表法,不用StringBuffer,用数组
  */
 public static void ten_two_select(int a){
  char[] two={'0','1'};
  char[]cunchu=new char[32];
  int add=cunchu.length;
  while(a!=0){
   int temp=a&1;
   cunchu[--add]=two[temp];
   a=a>>>1;
  }
  System.out.println(add);
  for (int i = add; i <cunchu.length; i++) {
   System.out.print(cunchu[i]+",");
  }
  
 }
 /*
  * 10------16
  */
 public static void shi_shiliu(int num){
  public_change(num, 15, 4);
 }
 /*
  * 10------8
  */
 public static void shi_ba(int num){
  public_change(num, 7, 3);
 }
 /*
  * 10------2
  */
 public static void shi_er(int num){
  public_change(num, 1, 1);
 }
 /*
  * 总结,抽取,查表法
  */
 public static void public_change(int num,int b,int right){
  char shuzu[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  char[] arr=new char[32];
  int move=arr.length;
  while(num!=0){
   int temp=num&b;
   arr[--move]=shuzu[temp];
   num=num>>>right;//
  }
  for (int i = move; i < arr.length; i++) {
   System.out.println(arr[i]);
  }
  
 }
}

                                                                            

                                          ----------------------    android培训      java培训   期待与您交流!    ----------------------   
                         详细请查看      http://edu.csdn.net/heima
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

King·Forward

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值