1

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;
        }
    }
Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120
Liam de Haas
  • 1,258
  • 3
  • 18
  • 39

5 Answers5

3

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.

Daniel
  • 21,933
  • 14
  • 72
  • 101
  • 1
    i did what you said just before you posated it, see my edit. thank you nonetheless – Liam de Haas Nov 22 '13 at 18:00
  • In the default clause, you are doing: `result = remainder + result`. Is that only to change the int to String ? You can do this clearer: `result = "" + remainder`. Or in this case with your version of `Integer.toString` – Daniel Nov 22 '13 at 18:07
  • how would i make it recursive if it returns a string and the parameter is an int? – Liam de Haas Nov 22 '13 at 18:09
  • Look closely at your function, the type of the returned value is not related to the type of the input parameter. – Daniel Nov 22 '13 at 18:13
  • I don't get it. i need the return type to be string otherwise a can't print the hex vaue an in order for the function to be recursive i need to let function call itself. it returns a string so how can the input parameter type differ fromn the return type? – Liam de Haas Nov 22 '13 at 18:17
  • No problem. Look, if you change your input parameter to int, and your recursive call is like this, would it work? `return Hexa(n/16) + result;` – Daniel Nov 22 '13 at 18:19
  • yes it does, guess that's because result is a string. now the whole `'F' = 'A' + (15 - 10)` thing, how does that work? – Liam de Haas Nov 22 '13 at 18:21
  • It's a way to turn numbers into letters by using their ASCII values. As it is out scope, check other posts about it [here is one](http://stackoverflow.com/questions/10813154/converting-number-to-letter) – Daniel Nov 22 '13 at 22:46
1

The code below may help you to solve your problem:

public static String decimalToAnyBase(int num,int base) {       
    if(num<base) {
        return num+"";
    }
    String result=null;
    int rem=num%base;
    String str=decimalToAnyBase(num/base, base);
    result=str+((rem>=10)? (char)(rem-10+'A')+"":rem);
    return result;
}
boutta
  • 24,189
  • 7
  • 34
  • 49
0
    import java.util.Scanner;

    public class Assign01_05 
    {
        static String res;
        public static void hex(int num) //125
        {
            if(num>=0 && num<10)
                System.out.print(num);
            else if(num>=10 && num<=15)
            {
                switch(num)
                {
                case 10:
                    System.out.print('A');
                    break;
                case 11:
                    System.out.print('B');
                    break;
                case 12:
                    System.out.print('C');
                    break;
                case 13:
                    System.out.print('D');
                    break;
                case 14:
                    System.out.print('E');
                    break;
                case 15:
                    System.out.println('F');
                    break;
                }
            }
            else
            {
                hex(num/16);
                hex(num%16);
            }
        }
        public static void main(String[] args) 
        {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter the Demical number :");
            int num=sc.nextInt();
            hex(num);
        }
    }

import java.util.Scanner;

public class Assign01_05 
{
    static String res;
    public static void hex(int num) //125
    {
        if(num>=0 && num<10)
            System.out.print(num);
        else if(num>=10 && num<=15)
        {
            switch(num)
            {
            case 10:
                System.out.print('A');
                break;
            case 11:
                System.out.print('B');
                break;
            case 12:
                System.out.print('C');
                break;
            case 13:
                System.out.print('D');
                break;
            case 14:
                System.out.print('E');
                break;
            case 15:
                System.out.println('F');
                break;
            }
        }
        else
        {
            hex(num/16);
            hex(num%16);
        }
    }
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Demical number :");
        int num=sc.nextInt();
        hex(num);
    }
}
Kai-Sheng Yang
  • 1,535
  • 4
  • 15
  • 21
0
import java.util.Scanner;

public class Ques5 {
    
    public static void hex(int n) {
        
        if(n>9&&n<=15) {
            System.out.printf("%c",'A'+(n-10));
        }
        else if(n>=0&&n<=9){
            System.out.printf("%d",n);
        }
        else {
            hex(n/16);
            hex(n%16);
        }       
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the decimal number : ");
        int i = sc.nextInt();
        System.out.println("The hexadecimal number is : ");
        hex(i);
        sc.close();
    }

}
fatih
  • 1,285
  • 11
  • 27
0
const char hex_string[17] = "0123456789ABCDEF";
void dec_to_hex(long long x){
    if(x == 0) return;
    dec_to_hex(x/16);
    printf("%c",hex_string[x%16]);
}

Same in Java