Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 8th, 2007, 10:14 AM   #11
gam3r
Newbie
 
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0 gam3r is on a distinguished road
Re: Decimal to binary help

I know it is frustrating trying to help someone like me but i'm trying so hard, i have limited java experience, just a beginner and i am tearing my hair out. I just don't know what to do.
__________________
biggest NOOB ever :P
gam3r is offline   Reply With Quote
Old Nov 8th, 2007, 11:50 AM   #12
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Re: Decimal to binary help

OMG! Just post your entire code that you have and the error messages you get. What is so confusing about my solution?
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Nov 8th, 2007, 3:56 PM   #13
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,453
Rep Power: 7 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Re: Decimal to binary help

ReggaetonKing's code works fine. Make sure you didn't copy-and-paste it into the middle of another one of your functions and be sure you are calling his function with proper parameters. If you are still confused, definitely post your code and errors that you receive. Oh, btw... his code is in fact using recursion... what exactly were you expecting?
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Nov 9th, 2007, 6:02 AM   #14
gam3r
Newbie
 
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0 gam3r is on a distinguished road
Re: Decimal to binary help

Ah sorry my bad i'm an idiot! I didn't read through it properly gahhhh! Sorry guys, im gonna go and have a tinker around and will get back to you.
__________________
biggest NOOB ever :P
gam3r is offline   Reply With Quote
Old Nov 11th, 2007, 3:47 PM   #15
gam3r
Newbie
 
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0 gam3r is on a distinguished road
Re: Decimal to binary help

Hey guys.

I have managed to get my code to work now, but there is one slight problem. The binary output is backwards i.e it reads from left to right instead of right to left like binary should so 25 is outputed as 100110 instead of 011001.

How do i get it to output in reverse order?

Here is my code:

public static String decTobin (int n){
	if (n==0){
		return "0";
	}
	if (n%2==0){
		return "0" + decTobin(n/2);
	}
	else {
		return "1" + decTobin(n/2); 
		}


}
__________________
biggest NOOB ever :P
gam3r is offline   Reply With Quote
Old Nov 11th, 2007, 5:00 PM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Decimal to binary help

Reverse the returned string, or do it another way.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 13th, 2007, 1:11 PM   #17
gam3r
Newbie
 
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0 gam3r is on a distinguished road
Re: Decimal to binary help

Quote:
Originally Posted by DaWei View Post
Reverse the returned string, or do it another way.
How can i do that? Can you give me any hints...?

My code actually doesn't save the output into a string, instead it just prints the answer out on the screen on the fly so how would i do it, would i have to implement a string to reverse it?

thanks
__________________
biggest NOOB ever :P
gam3r is offline   Reply With Quote
Old Nov 13th, 2007, 1:23 PM   #18
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Re: Decimal to binary help

[DELETED] *not even worth it.
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Nov 13th, 2007, 1:24 PM   #19
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Decimal to binary help

Yes, you could put it into a string and reverse it. That's accomplished by swapping the first and last characters, then the second and second-to-last characters, then the third and (you get the picture) until the swap reaches the middle.

Alternatively, you could do it another way. Consider your method: you are using the modulus operator (actually, the remainder operator) to test the least significant bit. You are then dividing by 2 to shift the number right, then testing the new LSB.

You COULD set up to test beginning with the MSB and work your way down to the LSB.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 14th, 2007, 2:24 AM   #20
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
Re: Decimal to binary help

Alternatively, you could do this depending on your specs:
	public static String decToBin( int n )
	{
		if (n==0)
			return "";

		if (n%2==0)
			return decToBin(n/2) + "0";
		else
			return decToBin(n/2) + "1";
	}
Harakim is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
linked list & binary tree questions n00b C++ 14 Nov 4th, 2006 2:24 AM
Java code to convert binary to decimal and hexadecimal to binary prince_haldir Java 1 Mar 7th, 2006 1:51 AM
Binary Ubert Other Programming Languages 8 Sep 23rd, 2005 10:09 AM
binary and ascii Nellie C++ 4 Jun 8th, 2005 1:39 PM
hex and decimal RGB transforming please help cloud- Visual Basic 5 Jan 17th, 2005 3:17 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:20 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC