Java implementation hexadecimal conversion to decimal example

Source: Internet
Author: User

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));

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.