Java 中的十六进制转十进制
Hexadecimal to decimal in Java
我的错误在哪里?
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;
}
当你运行
number = hex[help];
number 被分配了字符值 ('0'
),而不是数值 0。“0”的字符值为 48。
我的错误在哪里?
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;
}
当你运行
number = hex[help];
number 被分配了字符值 ('0'
),而不是数值 0。“0”的字符值为 48。