进制转换

进制转换

  1 package com.briup.day05;                                                                                                                     
  2 public class ZhuanHuan{
  3         public static void trans(int num,int base,int offset){
  4                 if(num == 0 ) System.out.println(0);
  5                 char [] chs ={
  6                         '0','1','2','3',
  7                         '4','5','6','7',
  8                         '8','9','A','B',
  9                         'C','D','E','F'
 10                 };
 11                 char [] arr = new char[32];
 12                 int pos = arr.length;//建立一个类似指针的东西
 13                 while (num != 0){
 14                         int temp = num & base;
 15                         arr[--pos] = chs [temp];
 16                         num = num >>> offset;
 17                 }
 18                 for(int i = pos;i<arr.length;i++){
 19                         System.out.print(arr[i]);
 20                 }
 21         }
 22         //十进制转二进制
 23         public static void toBin(int num){
 24                 trans(num,1,1);
 25         }
 26         //十进制转八进制
 27         public static void toBa(int num){
 28                 trans(num,7,3);
 29         }
 30         //十进制转十六进制
 31         public static void toHex(int num){
 32                 trans(num,15,4);
 33         }
 34         public static void main(String [] args){
 35                 toBin(6);
 36                 System.out.println();                                                                                                        
 37                 toBa(6);
 38                 System.out.println();
 39                 toHex(60);
 40                 System.out.println();
 41         }
 42 }            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值