![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Newbie
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#12 |
|
Sexy Programmer
|
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! |
|
|
|
|
|
#13 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#14 |
|
Newbie
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#15 |
|
Newbie
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#16 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#17 |
|
Newbie
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0
![]() |
Re: Decimal to binary help
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 |
|
|
|
|
|
#18 |
|
Sexy Programmer
|
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! |
|
|
|
|
|
#19 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#20 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
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";
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |