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

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

1. ## 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.  Reply With Quote

3. ## 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)
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)
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)
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)
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   Reply With Quote

4. ## 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! Originally Posted by dicdic 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)
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)
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)
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)
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   Reply With Quote

5. ## 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?  Reply With Quote

6. ## 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}; Originally Posted by dicdic 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?  Reply With Quote

7. ## 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.  Reply With Quote

8. ## 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  Reply With Quote

#### Tags for this Thread 