# How to make a Recursive method to change a Decimal to Hexadecimal

Page 1 of 1

## 3 Replies - 2623 Views - Last Post: 14 March 2009 - 03:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=92822&amp;s=19e5282a8b414e9be9d651341942b791&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bonkerspr

Reputation: 1
• Posts: 98
• Joined: 05-December 08

# How to make a Recursive method to change a Decimal to Hexadecimal

Posted 14 March 2009 - 10:59 AM

How to make a Recursive method to change a Decimal to Hexadecimal ? I am trying but i have problems to make this recursion. please help
Is This A Good Question/Topic? 0

## Replies To: How to make a Recursive method to change a Decimal to Hexadecimal

### #2 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• Posts: 2,392
• Joined: 16-August 08

## Re: How to make a Recursive method to change a Decimal to Hexadecimal

Posted 14 March 2009 - 11:09 AM

Post what you have so far:

[rules][/rules]

### #3 bonkerspr

Reputation: 1
• Posts: 98
• Joined: 05-December 08

## Re: How to make a Recursive method to change a Decimal to Hexadecimal

Posted 14 March 2009 - 03:08 PM

this is my code, i am having problems, i dont know where is the problem but it is not working. Please somebody help

```import java.util.*;

public class Hexconvertion {

public static void main(String[] args) {

Scanner key = new Scanner(System.in);

System.out.println("Please enter the number that you will convert to hex");
int decimalNum=key.nextInt();
}

public static void converttohex(int decimalNumber)
{
String calculated = "";
if(decimalNumber > 0)
{
converttohex(decimalNumber/16);
if(decimalNumber < 10)
{

String hexString = "";
int intChar = (decimalNumber / 16);
switch(intChar)
{
case 10: hexString = "A"; break;
case 11: hexString = "B"; break;
case 12: hexString = "C"; break;
case 13: hexString = "D"; break;
case 14: hexString = "E"; break;
case 15: hexString = "F"; break;
}
System.out.print(intChar+""+hexString);

}else
{
System.out.print((decimalNumber % 16));
}
}
}
}

```

### #4 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• Posts: 2,392
• Joined: 16-August 08

## Re: How to make a Recursive method to change a Decimal to Hexadecimal

Posted 14 March 2009 - 03:18 PM

Well you are making the method recursive and not returning any thing, so since the variable isn't a class variable then you have problems, you will have to return type String to build each part of Hex representation String.

An idea: check if number < 16 if it is and have a "helper" method to convert a number 0-15 to a String hex representation, if they are greater than that then do the % to get the correct and make the recursive call in that return

This post has been edited by BigAnt: 14 March 2009 - 03:32 PM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }