缺少一个步骤?使用数组(req'd)将 HexString 转换为 Decimal(Int)

问题描述 投票:0回答:1

因此,无论我输入什么,我编码的每个变体都会以 0 或 1 返回。请原谅篇幅/效率低下,只是想让它发挥作用。

v1 - if/else 测试

        System.out.println("Enter a hexadecimal String to convert to decimal (base_10)");
        while(true)
        {
            System.out.print("Hexadecimal >> ");
            String hex = scan.nextLine();
            
            String hexArray[] = hex.split("");
            //int hexNum[] = new int[hexArray.length];
            
            //System.out.println(Arrays.toString(hexArray)); //test
            int sum = 0;
            int exp = 0;
            for (int i = hexArray.length-1; i > 0; i--)
            {
                if(hexArray[i].equalsIgnoreCase("A"))
                    sum += 10 * Math.pow(16,exp);
                else if(hexArray[i].equalsIgnoreCase("B"))
                    sum += 11 * Math.pow(16,exp);
                else if(hexArray[i].equalsIgnoreCase("C"))
                    sum += 12 * Math.pow(16,exp);
                else if(hexArray[i].equalsIgnoreCase("D"))
                    sum += 13 * Math.pow(16,exp);
                else if(hexArray[i].equalsIgnoreCase("E"))
                    sum += 14 * Math.pow(16,exp);
                else if(hexArray[i].equalsIgnoreCase("F"))
                    sum += 15 * Math.pow(16,exp);
                else
                    sum += (Integer.parseInt(hexArray[i]) * Math.pow(16,exp));
                exp++;
            }
            System.out.println("\nHexadecimal: " + hex + "\t Decimal: " + sum);
        }

或者... v2 - 通过字符串切换

        System.out.println("Enter a hexadecimal String to convert to decimal (base_10)");
        while(true)
        {
            System.out.print("Hexadecimal >> ");
            String hex = scan.nextLine();
            
            String[] hexArray = hex.split("");
            
            System.out.println(Arrays.toString(hexArray)); //test
            int sum = 0;
            int exp = 0;
            for (int i = hexArray.length-1; i > 0; i--)
            {
                String s = hexArray[i];
                switch(s)
                {
                    case "A":   case "a":
                         sum += (10 * Math.pow(16,exp));
                         break;
                    case "B":   case "b":
                         sum += (11 * Math.pow(16,exp));
                         break;
                    case "C":   case "c":
                         sum += (12 * Math.pow(16,exp));
                         break;
                    case "D":   case "d":
                         sum += (13 * Math.pow(16,exp));
                         break;
                    case "E":   case "e":
                         sum += (14 * Math.pow(16,exp));
                         break;
                    case "F":   case "f":
                        System.out.println("hi");
                         sum += (15 * Math.pow(16,exp));
                         break;
                    default:    //i.e. NUMBERS
                         sum += ((Integer.parseInt(hexArray[i]) * Math.pow(16,exp)));
                }
                exp++;
            }
            System.out.println("\nHexadecimal: " + hex + "\t Decimal: " + sum);
            System.out.println("\n");
        }

v3 - 使用字符文字切换

        System.out.println("Enter a hexadecimal String to convert to decimal (base_10)");
        while(true)
        {
            System.out.print("Hexadecimal >> ");
            String hex = scan.nextLine();
            
            String[] hexArray = hex.split("");
            
            System.out.println(Arrays.toString(hexArray)); //test
            int sum = 0;
            int exp = 0;
            for (int i = hexArray.length-1; i > 0; i--)
            {
                char c = hexArray[i].charAt(0);
                switch(c)
                {
                    case '0':
                        break;
                    case '1':
                        sum += Math.pow(16,exp);
                        break;
                    case '2':
                        sum += 2 * Math.pow(16,exp);
                        break;
                    case '3':
                        sum += 3 * Math.pow(16,exp);
                        break;
                    case '4':
                        sum += 4 * Math.pow(16,exp);
                        break;
                    case '5':
                        sum += 5 * Math.pow(16,exp);
                        break;
                    case '6':
                        sum += 6 * Math.pow(16,exp);
                        break;
                    case '7':
                        sum += 7 * Math.pow(16,exp);
                        break;
                    case '8':
                        sum += 8 * Math.pow(16,exp);
                        break;
                    case '9':
                        sum += 9 * Math.pow(16,exp);
                        break;
                    case 'A':
                    case 'a':
                        sum += 10 * Math.pow(16,exp);
                        break;
                    case 'B':
                    case 'b':
                        sum += 11 * Math.pow(16,exp);
                        break;
                    case 'C':
                    case 'c':
                        sum += 12 * Math.pow(16,exp);
                        break;
                    case 'D':
                    case 'd':
                        sum += 13 * Math.pow(16,exp);
                        break;
                    case 'E':
                    case 'e':
                        sum += 14 * Math.pow(16,exp);
                        break;
                    case 'F':
                    case 'f':
                        sum += 15 * Math.pow(16,exp);
                        break;
                    default:
                        ;   //Invalid Character
                }
                exp++;
            }
            System.out.println("\nHexadecimal: " + hex + "\t Decimal: " + sum);

多种方法(如上所示),但首先将其拆分为数组并向后读取 R->L 以相应地增加指数。

java arrays hex
1个回答
0
投票

在代码的所有版本(v1、v2 和 v3)中,循环条件是 i > 0。这意味着当 i 变为 1 时循环将停止,然后它将跳过 hexArray 中的第一个元素(位于索引处) 0).

只需将循环条件更改为:

for (int i = hexArray.length - 1; i >= 0; i--)
© www.soinside.com 2019 - 2024. All rights reserved.