//十六进制数转二进制数importjava.util.Scanner;publicclassH_to_B{staticvoidHtoB_fun(Stringn){char[]ch=n.toCharArray();charstr;Strings=;intp=0;for(inti=0;ich.length;i++){str=ch[i];if(str='0'&&str='9'){Stringst=Character.toString(str);//字符转字符串p=Integer.valueOf(st).intValue();//字符串转整型}else{if(str=='A'){str=10;}if(str=='B'){str=11;}if(str=='C'){str=12;}if(str=='D'){str=13;}if(str=='E'){str=14;}if(str=='F'){str=15;}if(str=='.'){str=16;}p=(int)str;}switch(p){case0:s+=0000;break;case1:s+=0001;break;case2:s+=0010;break;case3:s+=0011;break;case4:s+=0100;break;case5:s+=0101;break;case6:s+=0110;break;case7:s+=0111;break;case8:s+=1000;break;case9:s+=1001;break;case10:s+=1010;break;case11:s+=1011;break;case12:s+=1100;break;case13:s+=1101;break;case14:s+=1110;break;case15:s+=1111;break;case16:s+=.;break;default:System.out.println(error!);break;}}System.out.println(结果为二进制:);System.out.println(s);}publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);System.out.println(请输入16进制数:);Stringstr=sc.nextLine();HtoB_fun(str);}}//二进制数转十六进制数importjava.util.Scanner;publicclassB_to_H{staticvoidBtoH_fun(Stringstr){Strings=;//s用来接收每位十六进制数的位数intchushihua=0;//判断是否有小数点intwest=0,wei=0;//小数点的位置intc;char[]ch1=str.toCharArray();for(inti=0;istr.length();i++){if(ch1[i]=='.'){chushihua=1;west=i;}}if(chushihua==1){intjud=4-(west%4);intjud1=4-((str.length()-1-west)%4);if(jud!=0&&jud!=4){for(inti=0;ijud;i++){str=0+str;}}if(jud1!=0&&jud1!=4){for(inti=0;ijud1;i++){str+=0;}}wei=str.indexOf(.)+1;str=str.replace(.,);}if(chushihua==0){intjudge=4-(str.length()%4);//前面该补judge个0if(judge!=0&&judge!=4){//当str.length()%4为0时,judge为4,所以要加judge!=4这个条件for(inti=1;i=judge;i++){str=0+str;}}}char[]ch=str.toCharArray();int[]a=newint[str.length()];for(inti=0;istr.length();i++){Stringst=Character.toString(ch[i]);//把字符转换为字符串a[i]=Integer.valueOf(st).intValue();//再把字符串转换为整型//System.out.print(a[i]);//用来测试是否在前面补零}for(inti=1;i=str.length();i++){if(i==wei&&wei!=0){s+=.;}if(i%4==0){c=a[i-1]*1+a[i-2]*2+a[i-3]*4+a[i-4]*8;if(c10){intb=a[i-1]*1+a[i-2]*2+a[i-3]*4+a[i-4]*8;s+=b+;}if(c=10){switch(c){case10:s+=A;break;case11:s+=B;break;case12:s+=C;break;case13:s+=D;break;case14:s+=E;break;case15:s+=F;break;}}}}System.out.println(结果为十六进制数:+s);}publicstaticvoidmain(String[]args){longstartTime=System.currentTimeMillis();Scannersc=newScanner(System.in);System.out.println(请输入一串二进制数字:);Stringstr=sc.nextLine();//Stringstr=101001011011111101010001;BtoH_fun(str);longendTime=System.currentTimeMillis();longusedTime=endTime-startTime;System.out.println(程序执行所用时间:+usedTime+毫秒!);}}