算法之将一个16进制的字符串转换成整数返回

public class HexToInt {
    public static void main(String[] args) {
        String str = "13abf";
        convert(str);
        
    }
    private static int convert(String str) {
        int len = str.length();
        int sum = 0;
        for (int i = 0; i < len; i++) {
            char c = str.charAt(len-1-i);
            int n = Character.digit(c, 16);
            sum += n*(1<<(4*i));
        }
        return sum;
    }
}

 

posted @ 2017-07-02 19:55  如果屈原会编程  阅读(435)  评论(0编辑  收藏  举报