public static int hexToDecimal(String hex){
int outcome = 0;
for(int i = 0; i < hex.length(); i++){
char hexChar = hex.charAt(i);
outcome = outcome * 16 + charToDecimal(hexChar);
}
return outcome;
}
/**
* @param: [c]
* @return: int
* @description:将字符转化为数字
/
public static int charToDecimal(char c){
if(c >= ‘A’ && c <= ‘F’)
return 10 + c - ‘A’;
else
return c - ‘0’;
}
//测试程序
public static void main(String… args) {
Scanner input = new Scanner(System.in);
String content = input.nextLine();
if(!content.matches("[0-9a-fA-F]")){
System.out.println(“输入不匹配”);
System.exit(-1);
}
//将全部的小写转化为大写
content = content.toUpperCase();
System.out.println(hexToDecimal(content);
java将十六进制转化为十进制
最新推荐文章于 2023-05-31 10:46:05 发布