I wrote some code to convert my hexadecimal display string to decimal integer. However, when input is something like 100a or 625b( something with letter) I got an error like
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value");
String s = sc.next();
//String s = "AD";
String s1 = s.toUpperCase();
int power = 0;
double result = 0;
char[] c = s1.toCharArray();
for (int i = c.length-1; i >=0 ; i--) {
boolean a = true;
switch(c[i]){
case 'A': c[i] = 10; a = false; break;
case 'B': c[i] = 11; a = false; break;
case 'C': c[i] = 12; a = false; break;
case 'D': c[i] = 13; a = false; break;
case 'E': c[i] = 14; a = false; break;
case 'F': c[i] = 15; a = false; break;
}
if(a==true){
result = result + (c[i]-48) * Math.pow(16, power++);
}else {
result = result + (c[i]) * Math.pow(16, power++);
}
}
System.out.println(result);
public class Hex2Decimal {
public static void hexDec(String num)
{
int sum=0;
int newnum = 0;
String digit = num.toUpperCase();
for(int i=0;i<digit.length();i++)
{
char c = digit.charAt(digit.length()-i-1);
if(c=='A')
{
newnum = 10;
}
else if(c=='B')
{
newnum = 11;
}
if(c=='C')
{
newnum = 12;
}
if(c=='D')
{
newnum = 13;
}
if(c=='E')
{
newnum = 14;
}
if(c=='F')
{
newnum = 15;
}
else
{
newnum = Character.getNumericValue(c);
}
sum = (int) (sum + newnum*Math.pow(16,i));
}
System.out.println(" HexaDecimal to Decimal conversion is" + sum);
}
public static void main(String[] args) {
hexDec("9F");
} }
Well, Mr.ajb has resolved and pointed out the error in your code.
Coming to the second part of the code, that is, converting a string with letters to decimal integer below is code for that,
import java.util.Scanner;
public class HexaToDecimal
{
int number;
void getValue()
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter hexadecimal to convert: ");
number = Integer.parseInt(sc.nextLine(), 16);
sc.close();
}
void toConvert()
{
String decimal = Integer.toString(number);
System.out.println("The Decimal value is : " + decimal);
}
public static void main(String[] args)
{
HexaToDecimal htd = new HexaToDecimal();
htd.getValue();
htd.toConvert();
}
}
You can refer example on hexadecimal to decimal for more information.
It looks like there's an extra space character in your string. You can use trim()
to remove leading and trailing whitespaces:
temp1 = Integer.parseInt(display.getText().trim(), 16 );
Or if you think the presence of a space means there's something else wrong, you'll have to look into it yourself, since we don't have the rest of your code.
void htod(String hexadecimal)
{
int h = hexadecimal.length() - 1;
int d = 0;
int n = 0;
for(int i = 0; i<hexadecimal.length(); i++)
{
char ch = hexadecimal.charAt(i);
boolean flag = false;
switch(ch)
{
case '1': n = 1; break;
case '2': n = 2; break;
case '3': n = 3; break;
case '4': n = 4; break;
case '5': n = 5; break;
case '6': n = 6; break;
case '7': n = 7; break;
case '8': n = 8; break;
case '9': n = 9; break;
case 'A': n = 10; break;
case 'B': n = 11; break;
case 'C': n = 12; break;
case 'D': n = 13; break;
case 'E': n = 14; break;
case 'F': n = 15; break;
default : flag = true;
}
if(flag)
{
System.out.println("Wrong Entry");
break;
}
d = (int)(n*(Math.pow(16,h))) + (int)d;
h--;
}
System.out.println("The decimal form of hexadecimal number "+hexadecimal+" is " + d);
}
Since there is no brute-force approach which (done with it manualy). To know what exactly happened.
Given a hexadecimal number
KₙKₙ₋₁Kₙ₋₂....K₂K₁K₀
The equivalent decimal value is:
Kₙ * 16ₙ + Kₙ₋₁ * 16ₙ₋₁ + Kₙ₋₂ * 16ₙ₋₂ + .... + K₂ * 16₂ + K₁ * 16₁ + K₀ * 16₀
For example, the hex number AB8C
is:
10 * 16₃ + 11 * 16₂ + 8 * 16₁ + 12 * 16₀ = 43916
Implementation:
//convert hex to decimal number
private static int hexToDecimal(String hex) {
int decimalValue = 0;
for (int i = 0; i < hex.length(); i++) {
char hexChar = hex.charAt(i);
decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
}
return decimalValue;
}
private static int hexCharToDecimal(char character) {
if (character >= 'A' && character <= 'F')
return 10 + character - 'A';
else //character is '0', '1',....,'9'
return character - '0';
}