Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Converting Hex to Decimal

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Converting Hex to Decimal

    hi... its me again...


    can someone help me? with this??

    /*Hexadecimal to Decimal

    Given a hexadecimal number as input. Convert this number to its decimal equivalent.
    Catch all exceptions that maybe caused by the program.
    It is up to you what will be the statements to be executed by the computer when an exception occurs.
    **/

    im having hard time implementing the exceptions... but i guess i can do anything about it.. its very hard to be a student
    please if anyone can help me


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Values of Input

    we have the same thread title huh... any way what do you got there so far?

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Values of Input

    Please post a new topic when you're asking a new question...

    I''m assuming that the number being inputted is in a String. In that case, you can use the Integer.parseInt() method;

    try
    {
    Integer.parseInt("3efd",16);
    }
    catch (NumberFormatException e)
    {
         System.out.println("Error! tried to parse an invalid number format");

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Values of Input

    helloworld can you please ,if it is ok.. can you post here the link of my previews post about conversion.. i dont know how to locate it easily. it may help him... the title is
    [SOLVED]numerical conversion...

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Values of Input

    hehe, I was trying to find the link to it, too

    Here it is: Numerical conversion methods

  6. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Converting Hex to Decimal

    yah thats it hehehehehe

  7. #7
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting Hex to Decimal

    thank you so much... i appreciate it....

  8. #8
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting Hex to Decimal

    how about the code in converting hexadecimal to decimal without using any packages? like
    .hexa??? .parseInt?

  9. #9
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default Re: Converting Hex to Decimal

    not sure if applicable or not, but this may spark an idea...maybe

    Decimal -> hex

    String[] allHex = new String[(16*16*16)-1];
     
    String key = "0123456789ABCDEFG";
     
    for(int i =0, int counter=0 ; i <16; ++i)
        for(int j =0; j <16; ++j)
            for(int k =0; k <16; ++k)
            {
            allHex[counter] = "" + key.charAt(i) +key.charAt(j)+key.charAt(k);
            ++counter;
            }
     
    // allHex now holds all hex values from 0 to FFF...for example allHex[123] = "07B"

    Maybe you can find a way to work backwards from this

    heres another solution to same problem i just solved..using modulus instead this time, and it can convert a single number inputted..instead of generating all values upto desired n
    http://www.javaprogrammingforums.com...cimal-hex.html
    Last edited by rsala004; November 10th, 2009 at 02:11 PM.

  10. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting Hex to Decimal

    import java.util.Scanner;


    public class N70HexadecimaltoDecimalConverter {

    /**
    * @param args
    */
    //Class for powered
    public static int pow(int i, int powerNum) {

    return (int) Math. pow( 16, powerNum);
    }

    public static void main(String[] args) {


    Scanner input = new Scanner(System.in);
    System.out.print("Enter HexDecimalNumber :" );

    String hdN=input.nextLine();
    long decNum=0;

    for(int i=0;i<hdN.length();i++){
    //define the power(exponent)
    int powerNum=hdN.length()-i-1;
    //extract the chars of String consecutively
    int de= hdN.charAt(i);
    //format the Hexadecimal to Decimal number
    switch(de){
    case '0':decNum= 0*pow(16,powerNum)+decNum;break;
    case '1':decNum= 1*pow(16,powerNum)+decNum;break;
    case '2':decNum= 2*pow(16,powerNum)+decNum;break;
    case '3':decNum= 3*pow(16,powerNum)+decNum;break;
    case '4':decNum= 4*pow(16,powerNum)+decNum;break;
    case '5':decNum= 5*pow(16,powerNum)+decNum;break;
    case '6':decNum= 6*pow(16,powerNum)+decNum;break;
    case '7':decNum= 7*pow(16,powerNum)+decNum;break;
    case '8':decNum= 8*pow(16,powerNum)+decNum;break;
    case '9':decNum= 9*pow(16,powerNum)+decNum;break;
    case'A':decNum= 10*pow(16,powerNum)+decNum;break;
    case'B':decNum= 11*pow(16,powerNum)+decNum;break;
    case'C':decNum= 12*pow(16,powerNum)+decNum;break;
    case'D':decNum= 13*pow(16,powerNum)+decNum;break;
    case'E':decNum= 14*pow(16,powerNum)+decNum;break;
    case'F':decNum= 15*pow(16,powerNum)+decNum;break;

    }
    }
    System.out.println("Decimal number is: "+decNum);
    }
    }
    Last edited by bogben; September 4th, 2011 at 04:23 PM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Converting Hex to Decimal

    Why not use an array vs that awful switch statement?

Similar Threads

  1. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  2. decimal to hex
    By rsala004 in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 3rd, 2009, 02:16 AM
  3. Replies: 4
    Last Post: May 1st, 2009, 04:32 PM
  4. [SOLVED] How to use decimal place when formatting an output?
    By napenthia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 27th, 2009, 04:17 AM
  5. Program to convert Hexadecimal to its Character equivalent
    By nathanernest in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2009, 04:12 AM