Skip to content
Advertisement

Convert Decimal to Hex using Recursive method Java

I need to make a recursive method that converts a decimal into hexadecimal. I can’t use Integer.toHexString. EDIT:I tried this code but it doesn’t work properly

public static String Hexa(String s) {
    String result = "";
    int n = Integer.parseInt(s);
    int remainder = n % 16;

    if (n == 0) {
        return Integer.toString(0);
    } else {
        switch (remainder) {
            case 10:
                result = "A" + result;
                break;
            case 11:
                result = "B" + result;
                break;
            case 12:
                result = "C" + result;
                break;
            case 13:
                result = "D" + result;
                break;
            case 14:
                result = "E" + result;
                break;
            case 15:
                result = "F" + result;
                break;
            default: result = Integer.toString(n/16) + result; break;
        }
        System.out.println(result);
        return Hexa(Integer.toString(n/16)) + result;
    }
}

Edit: Changed the default case and the if (n == 0) loop return statement and it works beautifully now.

new code:

 public static String Hexa(String s) {
        String result = "";
        int n = Integer.parseInt(s);
        int remainder = n % 16;

        if (n == 0) {
            return "";
        } else {
            switch (remainder) {
                case 10:
                    result = "A";
                    break;
                case 11:
                    result = "B";
                    break;
                case 12:
                    result = "C";
                    break;
                case 13:
                    result = "D";
                    break;
                case 14:
                    result = "E";
                    break;
                case 15:
                    result = "F";
                    break;
                default:
                    result = remainder + result;
                    break;
            }
            return Hexa(Integer.toString(n / 16)) + result;
        }
    }

Advertisement

Answer

The problem is in your default clause:

default: result = Integer.toString(n/16) + result; break;

it should read:

default: result = Integer.toString(remainder) + result; break;

That will make your program return “04D2”.

But there are several other corrections you can make:

  1. Stop converting back and forth to String. For example that same line can be just:

    default: result = remainder + result; break;

    Also, change your parameters time to int. If you do need to receive a String, then make this an auxiliary function and make your main function receive a String.

  2. You don’t need that breakat the end of your default

  3. You don’t need a switch. Isn’t 'F' = 'A' + (15 - 10) ? You can figure out how to make a formula that translates any number in the range [10,15] to its corresponding letter.

  4. Instead of Integer.toString(0) you can use "0" … but that isn’t even necessary, you can use "" to avoid that leading 0 in your output. If your are worried for handling the special case where the whole number is "0" add a special clause.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement