package com.pagination.plus.workTrain;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main0 {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\JavaData\tmp/input.txt")));
//BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String read = null;
//String[] hexTable = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
String hexTable = "0123456789ABCDEF";
while ((read=bufferedReader.readLine())!=null){
String hexNum = read.substring(2).toUpperCase();
int num=0;
for(int j=0, i=hexNum.length()-1;i>=0;i--,j++){
num += hexTable.indexOf(hexNum.charAt(j)) * (int)Math.pow(16,i);
}
System.out.println(num);
//System.out.println(Integer.parseInt(hexNum,16));
}
}
}
如果对api熟悉,直接使用java提供的可以一行搞定
这里需要记下parse的用法,Integer,Long,Double等包装类都提供了相应的方法,如果不涉及其他进制的转换,只是字符串样式的十进制数,直接使用valueOf就OK。
static Integer valueOf(String s)
Returns an Integer object holding the value of the specified String.
System.out.println(Integer.parseInt(hexNum,16));
注意:如果使用了parse,一定要注意转换数据的范围,
Integer.parseInt需要注意的最小值为
MIN_VALUE = 0x80000000;(负数在计算机中都是保存的补码)
最大值为
MAX_VALUE = 0x7fffffff;
一定要保证在这个数据间才行,否则会出现java.lang.NumberFormatException