3 Replies - 2204 Views - Last Post: 14 March 2009 - 03:18 PM Rate Topic: -----

#1 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 05-December 08

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

Post icon  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  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • 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]
Was This Post Helpful? 0
  • +
  • -

#3 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • 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));
			}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1