十六进字符串制转十进制

 

输入一个十六进制字符串,输出十进制字符串

输入:0xA
输出:10

public class Main {

	public static void main(String[] args) {	
		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNext()){
			StringBuffer sb = new StringBuffer();
			sb.append(scanner.next());
			String st = sb.substring(2);//此处输入的十六进制字符串形如0xA,故截去前两个字符
			double result = Method.convertto10(st);
			String r = String.valueOf(result).substring(0, String.valueOf(result).indexOf("."));
			System.out.println(r);
		}			
	}
	
}
class Method{

	public static double convertto10(String str){
		double result = 0;
		char ch[] = str.toCharArray();
		for(int i=0;i=ch[i]){
				result = result + (Integer.valueOf(ch[i])-55)*Math.pow(16, ch.length-1-i);
			}else if('a'ch[i]){
				result = result + (Integer.valueOf(ch[i])-87)*Math.pow(16, ch.length-1-i);
			}
			else{
				result = result + (Integer.valueOf(ch[i])-48)*Math.pow(16, ch.length-1-i);
			}
		}
		return result;
	}
    
}

Integer.valueOf('字符')得到该字符对应的ASCII码,

0——48

A——65

a——97

牛客网会出现eclipse编译通过但提交不通过的情况,用while(scanner.hasNext()){}包裹即可

你可能感兴趣的:(学习笔记)