- import java.util.*;
- import java.math.*;
- import java.io.*;
- class Radiux
- {
- /*
- * 其中字符串转换成二进制
- * 二进制转换成16进制可以继续封装
- */
- private String num;
- private StringBuilder SB = new StringBuilder();
- private char[] MyCode ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; //不说你也懂
- private int[] Bin = {1,2,4,8};//这个你也懂
- private String ans="";//保存结果
- Radiux(String num)//构造函数
- {
- this.num = num;
- }
- public void show()
- {
- char[] arr = num.toCharArray();
- int len = num.length();
- boolean flag = true;
- int Mod = 0,Tmp;
- ans = "";
- while(flag==true)//转换成二进制
- {
- if((arr[len-1]-'0')%2==0) SB.append("0");
- else SB.append("1");
- flag = false;
- Mod = 0;
- for(int i=0;i<len;i++)
- {
- Tmp = arr[i]-'0'+Mod*10;
- Mod = Tmp%2;
- Tmp/=2;
- if(Tmp!=0)flag = true;
- arr[i]= (char)(Tmp+'0');
- }
- }
- //System.out.println(SB.toString());
-
- for(int i=0;i<SB.length();i+=4)//把二进制转换成16进制 四个二进制位 可以转换成一位16进制
- {
- Tmp=0;
- for(int j=i;j<i+4&&j<SB.length();j++)
- Tmp += (SB.charAt(j)-'0')*Bin[j%4];
- ans = MyCode[Tmp] + ans;
- // System.out.println("Tmp = "+Tmp);
- // System.out.println();
- }
- SB.delete(0, len);//清空 StringBuilder
- System.out.println(ans);
- }
- }
- public class Cow
- {
- public static void main(String[] args)
- {
- Scanner cin = new Scanner(System.in);
- String str;
-
- while(cin.hasNext())
- {
- str = cin.next();
- Radiux R = new Radiux(str);
- R.show();
- }
- }
- }
复制代码 |