Java 不使用.parseInt()将十六进制转换为十进制;
我正在编写一些代码,将十六进制转换为十进制,而不使用Integer.parseInt(n,16)等内置java函数 这是我做的,但它不起作用:Java 不使用.parseInt()将十六进制转换为十进制;,java,Java,我正在编写一些代码,将十六进制转换为十进制,而不使用Integer.parseInt(n,16)等内置java函数 这是我做的,但它不起作用: public static int hexToDecimal(String hexInput) { String hexIn = hexInput.replace("", " ").trim(); Scanner hex = new Scanner(hexIn); int decimal = 0; int powe
public static int hexToDecimal(String hexInput) {
String hexIn = hexInput.replace("", " ").trim();
Scanner hex = new Scanner(hexIn);
int decimal = 0;
int power = 1;
while (hex.hasNext() == true) {
String temp = hex.next();
if (temp.equals("1") == true) {
decimal += 1 * power;
} else if (temp.equals("2") == true) {
decimal += 2 * power;
} else if (temp.equals("3") == true) {
decimal += 3 * power;
} else if (temp.equals("4") == true) {
decimal += 4 * power;
} else if (temp.equals("5") == true) {
decimal += 5 * power;
} else if (temp.equals("6") == true) {
decimal += 6 * power;
} else if (temp.equals("7") == true) {
decimal += 7 * power;
} else if (temp.equals("8") == true) {
decimal += 8 * power;
} else if (temp.equals("9") == true) {
decimal += 9 * power;
} else if (temp.equals("A") == true) {
decimal += 10 * power;
} else if (temp.equals("B") == true) {
decimal += 11 * power;
} else if (temp.equals("C") == true) {
decimal += 12 * power;
} else if (temp.equals("D") == true) {
decimal += 13 * power;
} else if (temp.equals("E") == true) {
decimal += 14 * power;
} else if (temp.equals("F") == true) {
decimal += 15 * power;
}
power = power * 16;
}
System.out.println(decimal);
return decimal;
}
能给我一些帮助吗?它似乎有一些基本的功能,但大多数输入都会中断。谢谢你的帮助 您必须反转输入字符串,因为大多数重要数字都在右边 put
hexInput=新的StringBuilder(hexInput).reverse().toString()代码>以反转钉住
或者简单地用另一种方式,比如
int power=Math.pow(16,hexInput.length()-1);
然后在循环结束时做
power/=16;
我将使用第二种方法,因为您不必执行额外的代码。您必须反转输入字符串,因为按照您的方式执行,大多数重要的数字都在右侧
puthexInput=新的StringBuilder(hexInput).reverse().toString()代码>以反转钉住
或者简单地用另一种方式,比如
int power=Math.pow(16,hexInput.length()-1);
然后在循环结束时做
power/=16;
我将使用第二种方法,因为您不必执行额外的代码。当您向右扫描时,您将逐步乘以16的更高幂。那正是你想要的。请尝试以下逻辑,这比您现在所做的要简单一些:
public static int hexToDecimal(String hexInput) {
int decimal = 0;
int len = hexInput.length();
for (int i = 0; i < len; ++i) {
char c = hexInput.charAt(i);
int cValue;
switch (c) {
case '1':
cValue = 1;
break;
case '2':
cValue = 2;
break;
. . .
default: // unexpected character
throw new IllegalArgumentException("Non-hex character " + c
+ " found at position " + i);
}
decimal = 16 * decimal + cValue;
}
return decimal;
}
public static int hexToDecimal(字符串hextInput){
整数小数=0;
int len=hexInput.length();
对于(int i=0;i
它会像你现在这样从左向右扫描,将已经处理的值乘以16,每遇到一个新的十六进制数字。当你向右扫描时,你会逐渐乘以16的更高幂。那正是你想要的。请尝试以下逻辑,这比您现在所做的要简单一些:
public static int hexToDecimal(String hexInput) {
int decimal = 0;
int len = hexInput.length();
for (int i = 0; i < len; ++i) {
char c = hexInput.charAt(i);
int cValue;
switch (c) {
case '1':
cValue = 1;
break;
case '2':
cValue = 2;
break;
. . .
default: // unexpected character
throw new IllegalArgumentException("Non-hex character " + c
+ " found at position " + i);
}
decimal = 16 * decimal + cValue;
}
return decimal;
}
public static int hexToDecimal(字符串hextInput){
整数小数=0;
int len=hexInput.length();
对于(int i=0;i
它会像现在这样从左到右扫描,将已经处理的值乘以16,每遇到一个新的十六进制数字。什么中断?你收到错误信息了吗?如果是这样的话,你会得到什么?结果是程序运行时没有错误,但计算错误。就像输入3A应该输出58,但它实际上输出163。或者ABC应该输出2748,但它输出3258。你应该把它放在你的问题中。什么中断?你收到错误信息了吗?如果是这样的话,你会得到什么?结果是程序运行时没有错误,但计算错误。就像输入3A应该输出58,但它实际上输出163。或者ABC应该输出2748,但它输出3258。你应该把它放在你的问题中。