Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem with converting to hex to decimal

$
0
0
can anyone stop the mistake in my code

for some reason giving this method AA returns 160 not 170

/** This method converts a string from hexadecimal characters to a decial number
  * @since 			23/1/13
  * @version		1.0
  * @author			Dale Snowdon <dksnowdon@gmail.com>
  * @param inputIn : String		a string of hexadecimal characters
  * @return decimal: int	decimal number form the inputed string
  */
  	public static double hexConvert(String inputIn)
  	{
  		 int len = inputIn.length();
  		 
  		 double[] decimalValue = new double[len];
  		 
  		 int power;
  		 
  		 int j = 0;
  		 for (int i = len-1 ; i > 0 ;i--)
  		 {
  		 	 // loop though sting backwards
  		 	 power = i;
  		 	 
  		 	 decimalValue[j] = getHexNum(inputIn.charAt(j)) * Math.pow(16,power);
  		 	 
  		 	 System.out.println(decimalValue[j]);
  		 	 
  		 	 j++; // incerment foward pointer
  		 }
  		 
  		 double totalDecimalValue = 0;
  		 
  		 for (double a : decimalValue)
  		 	 totalDecimalValue = totalDecimalValue + a;
  		 
  		 System.out.println(totalDecimalValue);
  		 
  		 return totalDecimalValue;
  		
  	} // end method for hex convert
	   



/** Name: 			getHexNum
  * @since 			17/12/12
  * @version		1.1
  * @author			Dale Snowdon <dksnowdon@gmail.com>
  *	@param 			a a hexadecimal character
  * @return			the decimal value
  */
	public static int getHexNum(char a)
	{
		char[] hexValue = new char[16];// Create list full of the accsepted	
										// Values
			hexValue[0] = '0'; hexValue[1] = '1'; hexValue[2] = '2';
			hexValue[3] = '3'; hexValue[4] = '4'; hexValue[5] = '5';
			hexValue[6] = '6'; hexValue[7] = '7'; hexValue[8] = '8';
			hexValue[9] = '9'; hexValue[10] = 'A'; hexValue[11] = 'B';
			hexValue[12] = 'C'; hexValue[13] = 'D'; hexValue[14] = 'E';
			hexValue[15] = 'F';
		// Finish list
		
		int decimalReprasentation = 0;
		
		for (int i = 0;i<16;i++)
		{
			if (hexValue[i] == a)
			{
				decimalReprasentation = i;
				break;
			}
		}
		
		return decimalReprasentation;
		
	} // end method for getHexNum 



any help is appreciated
thanks dale

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>