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 7 of 7

Thread: Convert hexadecimal to decimal WITHOUT using integer.parseInt method

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Convert hexadecimal to decimal WITHOUT using integer.parseInt method

    Hey everyone I'm new to this site so sorry if I do anything noobish.

    So I have to write a java program that converts hexadecimals to decimals without using the whole "integer.parseInt(AB1, 16)" method. I tried looking up how to do this but every forum/site I went to used this same method. Thanks in advance for the help.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Convert hexadecimal to decimal WITHOUT using integer.parseInt method

    Hi,
    Here is my idea.
    First you must know/declare that each digit in hex corresponds a decimal/binary value
    so let say i have this kind of matrix:
    0=0
    1=1
    2=2
    3=3
    .
    .
    .
    .
    .
    A=10
    B=11
    .
    .
    F=15
    Get it?

    Next, I'll get each character starting from right:
    Let say I have this Hex number = AB2D
    I'll declare a exponent with initial value of 0
    I'll declare a integer with initial value of 0
    Get each character from right:

    first we get D
    D = 13
    multiply it by 16^exponent (exponent = 0 here)
    answer is 13
    add it to integer variable
    integer variable now is 13

    next we get 2
    2 = 2
    add 1 to exponent
    multiply 2 by 16^exponent (exponent = 1 here)
    answer is 32
    add it to integer variable
    integer variable now is 45

    next we get B
    B = 11
    add 1 to exponent
    multiply 11 by 16^exponent (exponent = 2 here)
    answer is 2816
    add it to integer variable
    integer variable now is 2861

    lastly we get A
    A = 10
    add 1 to exponent
    multiply 10 by 16^exponent (exponent = 3 here)
    answer is 40960
    add it to integer variable
    integer varialbe now is 43821

    and if you check in your calculator, AB2D = 43821
    hope that will help you, you just need to be creative on thinking how to put it in programming part
    Obviously you will need a loop

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Convert hexadecimal to decimal WITHOUT using integer.parseInt method

    Get! Thanks for the reply and for all your help! I understand the math portion of this question (how to conver hexadecimals to decimals) but I am a real amateur at programming. I actually kind of tried the method you are describing but don't really know how to do it. For example how do I declare so that the program read A as 10 B as 11 etc? And how do I get it to read whatever the input puts in from right to left? Thanks again!











    Quote Originally Posted by dicdic View Post
    Hi,
    Here is my idea.
    First you must know/declare that each digit in hex corresponds a decimal/binary value
    so let say i have this kind of matrix:
    0=0
    1=1
    2=2
    3=3
    .
    .
    .
    .
    .
    A=10
    B=11
    .
    .
    F=15
    Get it?

    Next, I'll get each character starting from right:
    Let say I have this Hex number = AB2D
    I'll declare a exponent with initial value of 0
    I'll declare a integer with initial value of 0
    Get each character from right:

    first we get D
    D = 13
    multiply it by 16^exponent (exponent = 0 here)
    answer is 13
    add it to integer variable
    integer variable now is 13

    next we get 2
    2 = 2
    add 1 to exponent
    multiply 2 by 16^exponent (exponent = 1 here)
    answer is 32
    add it to integer variable
    integer variable now is 45

    next we get B
    B = 11
    add 1 to exponent
    multiply 11 by 16^exponent (exponent = 2 here)
    answer is 2816
    add it to integer variable
    integer variable now is 2861

    lastly we get A
    A = 10
    add 1 to exponent
    multiply 10 by 16^exponent (exponent = 3 here)
    answer is 40960
    add it to integer variable
    integer varialbe now is 43821

    and if you check in your calculator, AB2D = 43821
    hope that will help you, you just need to be creative on thinking how to put it in programming part
    Obviously you will need a loop

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Convert hexadecimal to decimal WITHOUT using integer.parseInt method

    Okay, lets discuss how to do it.
    Actually we can determine the value of A=10 or B=11 just by using its ASCII decimal value.
    But we will not use that so you wont get confused.

    First I need to know if you already know about some classes in Java that can help us with this problem.
    Did you know about Map? HashMap, LinkedHashMap, etc.
    If no, do you know about Arrays? like String array or integer array?

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Convert hexadecimal to decimal WITHOUT using integer.parseInt method

    Hey thanks for the reply. I am not familiar with map hash map or linkedhashmap but I do know what an integer array is. When you put multiple integers into one variable correct? Such as int apples[]{83,28,74,9,2};






    Quote Originally Posted by dicdic View Post
    Okay, lets discuss how to do it.
    Actually we can determine the value of A=10 or B=11 just by using its ASCII decimal value.
    But we will not use that so you wont get confused.

    First I need to know if you already know about some classes in Java that can help us with this problem.
    Did you know about Map? HashMap, LinkedHashMap, etc.
    If no, do you know about Arrays? like String array or integer array?

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Convert hexadecimal to decimal WITHOUT using integer.parseInt method

    If the hexadecimal number is a string you could use the index of each character for your math and as you loop over all the characters you can use a switch statement with 16 cases so you never have to convert anything.

  7. #7
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Convert hexadecimal to decimal WITHOUT using integer.parseInt method

    okay, what we are going to do is to create an array of string or character.
    and that array will consist of 0-9, A-F.
    but make sure that you use a uniform case for letters, if A is in uppercase, the rest must be in uppercase also.
    something like this char[] hex = {'0','1','2'......'F'}
    as you noticed char 0 is at index 0, char A is at index 10,
    char F is at index 15. did you see the pattern/logic here?
    what we are going to do is get their index, so char B is equal to 11 right?
    that way we have now a hex to binary converter (single digit)
    how are going get the index of char hex?
    well you can do some algorithm.
    but the easiest way to get it is using the binarySearch method of Arrays class in java
    kindly visit this link and see how binarySearch works for arrays Arrays (Java Platform SE 8 )

    --------------
    actually we can do it without using array.
    we can do the convertion just by getting its ASCII.
    but for now, we are going to start with basics.
    --------------

    If the hexadecimal number is a string you could use the index of each character for your math and as you loop over all the characters you can use a switch statement with 16 cases so you never have to convert anything.
    yeah we can use switch cases here.
    but it will be easier to use the binarySearch as what I said in the above statement.
    the advantage is it will make the code shorter and will help the programmer learn/imporve on how to construct an algorithm. 16 switch cases is too long codes just to convert hex to decimal.

    And how do I get it to read whatever the input puts in from right to left? Thanks again!
    do you know how to get characters from left? using loop? do you know how to use toCharArray method of String? do you know how to create a loop?
    actually it is not necessary to read from right to left, we can also do the conversion from left to rigth, but reading it from left will make us add another statement that will compute on what number should the exponent starts. as you noticed, reading it from right will make our exponent equal to 0
    Last edited by dicdic; March 12th, 2014 at 07:16 PM.

Similar Threads

  1. Converting from decimal to hexadecimal
    By BTroj in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2014, 09:55 PM
  2. Integer.parseInt problem
    By 3therk1ll in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 28th, 2013, 05:28 AM
  3. convertion hexadecimal to decimal and binary to decimal
    By Md.Ashraful Haque in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 07:30 AM
  4. Use of Integer.parseInt
    By tarkal in forum Java SE APIs
    Replies: 3
    Last Post: September 8th, 2011, 03:37 AM
  5. Using Integer.parseInt
    By Fraz in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 07:50 AM

Tags for this Thread