Integer Wrapper Class

Convert hexadecimal number to decimal number example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

1 Comment

  • to convert hexadecimal to decimal without using any built in functions

    import java.util.Scanner;

    ?

    ?

    public class hex {

    static long dec=0;

    static long squ(int i)

    {

    long pow=16;

    if(i==0)

    {

    System.out.println(“return value********”+1);

    return 1;

    }

    else if(i==1)

    {

    System.out.println(“return value******”+16);

    return pow;

    }

    else

    {

    for(int k=2;k<=i;k++)

    {

    pow=pow*16;

    }

    System.out.println(“return value********”+pow);

    return pow;

    }

    }

    public static void main(String[] args) {

    Scanner so=new Scanner(System.in);

    System.out.println(“enter the hexa decimal no”);

    String hx=so.next();

    hx.toLowerCase();

    char c[]=hx.toCharArray();

    int j=c.length;

    int x=j;

    int i=0;

    /*System.out.println(j);

    for(int k=0;k<j;k++) {=”” system.out.println(c[k]);=”” }*=”” j–;=”” while(j=””>=0)

    {

    if(c[j]==’a’|c[j]==’b’|c[j]==’c’|c[j]==’d’|c[j]==’e’|c[j]==’f’|c[j]==’1’|c[j]==’2’|c[j]==’3’|c[j]==’4’|c[j]==’5’|c[j]==’6’|c[j]==’7’|c[j]==’8’|c[j]==’9′)

    {

    j–;

    }

    else

    {

    i++;

    break;

    }

    }

    if(i>0)

    {

    System.out.println(“its not hex decimal no”);

    }

    else

    {

    System.out.println(“it s hex decimal no”);

    x–;

    int xy=0;

    while(x>=0)

    {

    long z=squ(xy);

    ++xy;

    char r=c[x];

    String s=””+r;

    System.out.println(“current char****”+s);

    switch(s)

    {

    case “a”: dec=dec+(10*z);

    break;

    case “b”: dec=dec+(11*z);

    break;

    case “c”: dec=dec+(12*z);

    break;

    case “d”: dec=dec+(13*z);

    break;

    case “e”: dec=dec+(14*z);

    break;

    case “f”: dec=dec+(15*z);

    break;

    case “1”: dec=dec+(1*z);

    break;

    case “2”: dec=dec+(2*z);

    break;

    case “3”: dec=dec+(3*z);

    break;

    case “4”: dec=dec+(4*z);

    break;

    case “5”: dec=dec+(5*z);

    break;

    case “6”: dec=dec+(6*z);

    break;

    case “7”: dec=dec+(7*z);

    break;

    case “8”: dec=dec+(8*z);

    break;

    case “9”: dec=dec+(9*z);

    break;

    case “0”: dec=dec+(0*z);

    break;

    default:System.out.println(“cant find****”+s);

    break;

    }

    System.out.println(“cur dec value—–“+dec);

    System.out.println(“**************************”);

    x–;

    }

    System.out.println(“decimal equ is*****”+dec);

    }

    /*String hx=so.next();

    long i=Long.parseLong(hx);

    //double i=Double.POSITIVE_INFINITY;

    System.out.println(i);

    }

    */

    }

    }

Sponsors

Facebook Fans

Online Shopping – Flipkart