在Java中将十六进制转换为十进制

时间:2018-09-29 23:44:06

标签: java

我一直在做一个程序,将十六进制数转换为十进制数并返回。我整天都被这个问题困扰。我有以下代码:

    for (String element: list) {
        double number = 0;
        int i=0;
        while(i<element.length()) {
            char character = element.charAt(i);
            if (character=='A' | character== 'a') {
                int a= (int) (10*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character== 'B' | character =='b') {
                int a= (int) (11*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character== 'C' | character =='c') {
                int a= (int) (12*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character== 'D' | character =='d') {
                int a= (int) (13*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character== 'E' | character =='e') {
                int a= (int) (14*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character== 'F' | character =='f') {
                int a= (int) (15*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {

            if (character =='1') {
                int a= (int) (1*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character =='2') {
                int a= (int) (2*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {

            if (character =='3') {
                int a= (int) (3*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character =='4') {
                int a= (int) (4*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character =='5') {
                int a= (int) (5*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character =='6') {
                int a= (int) (6*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character =='7') {
                int a= (int) (7*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character =='8') {
                int a= (int) (8*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character=='9') {
                int a= (int) (9*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
            else {
            if (character =='0') {
                int a= (int) (0*(Math.pow((element.length()-(i+1)), 16)));
                number= number+a;
            }
        }}}}}}}}}}}}}}}
            i++;
            }

我知道它相当大而且很笨。我面临的问题是,当我运行它并放入AAA之类的数字时,它将抛出A000A十六进制值。或者,如果我输入123,它将以十六进制形式抛出10002的值。预先感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

是执行自己的实现的任务,还是只需要一个转换器? 如果您只需要转换器,可以执行Integer.parseInt("a",16)

已编辑:一般建议 为了使您的代码更具可读性,我建议在类内部创建私有静态映射,如下所示:

    private static final Map<Character, Integer> dictionary = new HashMap<Character, Integer>();
    static {
        myMap.put("A", 10);
        myMap.put("B", 11);
        //other code
    }

这样您就可以摆脱代码重复:

   Integer multiplier = dictionary.get(Character.toUpperCase(character))
   if(multiplier == null){/* do something */}
   int a= (int) (multiplier*(Math.pow(16, (element.length()-(i+1)))));
   number= number+a;

答案 1 :(得分:0)

您可以使用String方法toLowerCase()element.toLowerCase()改进程序,因此可以删除双重验证。您也可以使用Integer方法将十六进制转换为十进制,反之亦然:

Integer.parseInt(yourString, 16);

Integer.parseToHex(yourString);

答案 2 :(得分:0)

主要问题是您已经颠倒了Math.pow参数的顺序。

您执行的是34 hex = 3*16^1 + 4*16^0 = 52,而不是34 hex = 3*1^16 + 4*0^16 = 3,这显然会导致错误的结果。

翻转订单,例如:

        if (character=='A' | character== 'a') {
            int a= (int) (10*(Math.pow(16, (element.length()-(i+1)))));
            number= number+a;
        }