Recently, the younger brother wrote a hexadecimal conversion to decimal Java program, the general idea of this algorithm is to convert the number of hexadecimal input to 2, and then convert 2 to decimal algorithm. The time efficiency of the algorithm is not very good, you predecessors have any good suggestions hope that we do not hesitate to enlighten ah ~ Little brother here Thank you ~
Here's the Java code I wrote:
public class Dtoten {
/**
* @param args First converts hexadecimal to 2
*/
Public String Dtotwo (char[] a) {
String str= "";
for (int i=0;i<a.length;i++) {
Switch (A[i]) {
Case ' 0 ': str+= "0000";
Case ' 1 ': str+= "0001";
Case ' 2 ': str+= "0010";
Case ' 3 ': str+= "0011";
Case ' 4 ': str+= "0100";
Case ' 5 ': str+= "0101";
Case ' 6 ': str+= "0110";
Case ' 7 ': str+= "0111";
Case ' 8 ': str+= "1000";
Case ' 9 ': str+= "1001";
Case ' A ': str+= "1010";
Case ' B ': str+= "1011";
Case ' C ': str+= "1100";
Case ' D ': str+= "1101";
Case ' E ': str+= "1110";
Case ' F ': str+= "1111";
Default
Break
}
}
return str;
}
/*
* Convert 2 to Decimal
*/
public int Twototen (String a) {
Char[] C=a.tochararray ();
int sum=0;
int s;
int i=0;
Note the following conversions
for (; i<c.length-1;i++) {//2 at the end of the progress thought extra consideration
S=1;
if (c[i]== ' 1 ') {
for (int j=0;j<c.length-i-1;j++) {
s=s*2;
}
Sum+=s;
}
}
if (c[c.length-1]== ' 1 ') {
Sum+=1;
}
return sum;
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
String d= "12AB";
Char[] A=d.tochararray ();
Dtoten ttt=new Dtoten ();
String c=ttt. Dtotwo (a);
System.out.println (Ttt.twototen (c));
}
}