import java.util.Scanner;
class java
{
public static void main(String[] args) //十六进制转十进制
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a hex number :");
String hex = input.nextLine();
System.out.println("The decimal value for hex number " +
hex + " is " + hexToDecimal(hex.toUpperCase()));
}
//定义一个方法
public 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;
}
public static int hexCharToDecimal(char ch)
{
if (ch >= 'A' && ch <= 'Z')
return 10 + ch - 'A';
else
return ch - '0';
}
}
java十六进制转十进制
最新推荐文章于 2020-09-24 21:06:59 发布