有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中十六进制到十进制的转换

我的错误在哪里

The results are often 48 to high. For the 0 case i will add an If-statement. I want the stay with both loops if it is possible :)

public static int HexadecimalToDecimal(String hex1) {

        char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

        int intCache = 0;
        int runner = 0;
        int help = 0;
        int number = 0;

        for (int i = hex1.length(); i > 0; i--) {


            while (hex1.charAt(i - 1) != hex[help]) {
                help++;
                number = hex[help];
            }
            intCache += number * (Math.pow(16, runner));
            runner++;
        }

        return intCache;

    }

共 (1) 个答案

  1. # 1 楼答案

    当你跑的时候

                number = hex[help];
    

    数字分配的是字符值('0'),而不是数字值0。“0”的字符值为48