Reputation:
Where is my mistake ?
The results are often 48 to high. For the 0 case i will add an If-statement. I want the stay with both loops if it is possible :)
public static int HexadecimalToDecimal(String hex1) {
char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
int intCache = 0;
int runner = 0;
int help = 0;
int number = 0;
for (int i = hex1.length(); i > 0; i--) {
while (hex1.charAt(i - 1) != hex[help]) {
help++;
number = hex[help];
}
intCache += number * (Math.pow(16, runner));
runner++;
}
return intCache;
}
Upvotes: 1
Views: 81
Reputation: 75356
When you run
number = hex[help];
number is assigned the character value ('0'
), not the numeric value 0. The character value for '0' is 48.
Upvotes: 2