Search code examples
javaarrayshex

Missing a step? Using arrays (req'd) to convert HexString to Decimal (Int)


So every variation I've coded keeps coming back with 0 or 1 regardless of what I input. Excuse the length/inefficiency, just trying to make it work.

v1 - if/else tests

        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);
        }

Alternatively... v2 - switch by String

        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 - switch with char-literals

        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);

Multiple ways (shown above) , but initially splitting it into an array and reading backwards R->L to increment the exponent accordingly.


Solution

  • In all versions of your code (v1, v2, and v3), the loop condition is i > 0. This means that the loop will stop when i becomes 1, and then it will skip the first element in your hexArray (at index 0).

    Simply change the loop condition to this:

    for (int i = hexArray.length - 1; i >= 0; i--)