# Convert hexadecimal to decimal WITHOUT using integer.parseInt method

• March 12th, 2014, 01:32 AM
carlos_canas94
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.
• March 12th, 2014, 02:30 AM
dicdic
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)
integer variable now is 13

next we get 2
2 = 2
multiply 2 by 16^exponent (exponent = 1 here)
integer variable now is 45

next we get B
B = 11
multiply 11 by 16^exponent (exponent = 2 here)
integer variable now is 2861

lastly we get A
A = 10
multiply 10 by 16^exponent (exponent = 3 here)
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 :)
• March 12th, 2014, 02:59 AM
carlos_canas94
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
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)
integer variable now is 13

next we get 2
2 = 2
multiply 2 by 16^exponent (exponent = 1 here)
integer variable now is 45

next we get B
B = 11
multiply 11 by 16^exponent (exponent = 2 here)
integer variable now is 2861

lastly we get A
A = 10
multiply 10 by 16^exponent (exponent = 3 here)
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 :)

• March 12th, 2014, 03:33 AM
dicdic
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.
If no, do you know about Arrays? like String array or integer array?
• March 12th, 2014, 10:26 AM
carlos_canas94
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
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.
If no, do you know about Arrays? like String array or integer array?

• March 12th, 2014, 02:53 PM
Harry Blargle
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.
• March 12th, 2014, 07:01 PM
dicdic
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.
--------------

Quote:

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.

Quote:

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