# Converting Hex to Decimal

• November 9th, 2009, 12:22 AM
r2ro_serolf
Converting Hex to Decimal
hi... its me again...

can someone help me? with this??

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
• November 9th, 2009, 02:28 AM
chronoz13
Re: Values of Input
we have the same thread title huh... any way what do you got there so far?
• November 9th, 2009, 02:28 AM
helloworld922
Re: Values of Input

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

Code :

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

• November 9th, 2009, 02:31 AM
chronoz13
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
Quote:

[SOLVED]numerical conversion...
• November 9th, 2009, 02:34 AM
helloworld922
Re: Values of Input
hehe, I was trying to find the link to it, too :P

Here it is: Numerical conversion methods
• November 9th, 2009, 02:45 AM
chronoz13
Re: Converting Hex to Decimal
yah thats it hehehehehe :))
• November 9th, 2009, 03:31 AM
r2ro_serolf
Re: Converting Hex to Decimal
thank you so much... i appreciate it....
• November 9th, 2009, 03:34 AM
r2ro_serolf
Re: Converting Hex to Decimal
how about the code in converting hexadecimal to decimal without using any packages? like
.hexa??? .parseInt?
• November 10th, 2009, 01:07 PM
rsala004
Re: Converting Hex to Decimal
not sure if applicable or not, but this may spark an idea...maybe

Decimal -> hex

Code :

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
• September 4th, 2011, 03:13 PM
bogben
Re: Converting Hex to Decimal
import java.util.Scanner;

/**
* @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);
}
}
• September 4th, 2011, 04:29 PM
Norm
Re: Converting Hex to Decimal
Why not use an array vs that awful switch statement?