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
Re: Converting Hex to Decimal
yah thats it hehehehehe :))
Re: Converting Hex to Decimal
thank you so much... i appreciate it....
Re: Converting Hex to Decimal
how about the code in converting hexadecimal to decimal without using any packages? like
.hexa??? .parseInt?
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
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);
}
}
Re: Converting Hex to Decimal
Why not use an array vs that awful switch statement?