943,910 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1507
  • C++ RSS
Mar 3rd, 2009
0

Decimal to Binary Conversation stored into a char array

Expand Post »
Hello, I am trying to create a program where I can work with an array of char that contain either 0 or 1 representing a decimal number that the user enters.

Basically I need to convert a decimal number into binary and store it into a char array. I wrote this program but it is outputting something weird.

c++ Syntax (Toggle Plain Text)
  1. char binary(int number)
  2. {
  3. int temp = 0, c = 0;
  4. char buffer;
  5.  
  6. if (number % 2 == 0)
  7. {
  8. temp = number / 2;
  9. }
  10. else
  11. {
  12. number = number - 1;
  13. temp = number / 2;
  14. }
  15.  
  16. if (temp % 2 == 0)
  17. {
  18. buffer = '0';
  19. }
  20. else
  21. {
  22. buffer = '1';
  23. }
  24.  
  25. return buffer;
  26. }
  27.  
  28. int main()
  29. {
  30. int num;
  31.  
  32. cin >> num;
  33.  
  34. while(num != 0)
  35. {
  36. cout << binary(num);
  37.  
  38. if (num % 2 == 0)
  39. {
  40. num = num / 2;
  41. }
  42. else
  43. {
  44. num = num - 1;
  45. num = num / 2;
  46. }
  47. }
  48.  
  49. return 0;
  50. }
  51.  
  52. Can someone help me out??
Reputation Points: 76
Solved Threads: 6
Junior Poster
djextreme5 is offline Offline
103 posts
since Mar 2009
Mar 3rd, 2009
0

Re: Decimal to Binary Conversation stored into a char array

try something along the lines of itoa which should allow you to use base 2 to convert it to binary

it would look like so: itoa(temp, chararray, 2)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rgfirefly24 is offline Offline
3 posts
since Mar 2009
Mar 3rd, 2009
0

Re: Decimal to Binary Conversation stored into a char array

I cannot use itoa();

I already did that and it won't compile on my teacher's computer. Apparently she is using some website's judging process and it has a g++ compiler. Since itoa() is not a standard function, g++ doesn't support it.
Reputation Points: 76
Solved Threads: 6
Junior Poster
djextreme5 is offline Offline
103 posts
since Mar 2009
Mar 3rd, 2009
0

Re: Decimal to Binary Conversation stored into a char array

uhmmm... Why don't you just loop through all the bits in the value shifting it over, then checking it with a logic AND(mask 1). Add "1" to it if it's true, else "0".
Simple?
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Mar 3rd, 2009
0

Re: Decimal to Binary Conversation stored into a char array

uhmmm... Why don't you just loop through all the bits in the value shifting it over, then checking it with a logic AND(mask 1). Add "1" to it if it's true, else "0".
Simple?
What do you mean?
Reputation Points: 76
Solved Threads: 6
Junior Poster
djextreme5 is offline Offline
103 posts
since Mar 2009
Mar 3rd, 2009
0

Re: Decimal to Binary Conversation stored into a char array

Lets say you have the value ten, and in binary that is represented as "00001010." Mask(AND operator) all the bits before the first one and the truth is now false, place a zero to the array; shift the original value right once and mask again, this time the truth is true, add an "1"; repeat.

Well the array would look like "01010000" if you did that, there's an easy fix or just reverse it. If that's to much for you, can you use C++'s bitset class container?
Last edited by MosaicFuneral; Mar 3rd, 2009 at 10:06 pm.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Mar 3rd, 2009
0

Re: Decimal to Binary Conversation stored into a char array

just write your own itoa.
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. string itoa(const int &integer,int base=10)
  7. {
  8. if (integer==0) return string("0");
  9.  
  10. string a;
  11. int start, digits, piece;
  12.  
  13. //count digits
  14. digits=0;
  15. piece=((integer<0)? 0-integer : integer);
  16. while( piece > 0 )
  17. {
  18. piece-= (piece%base);
  19. piece/=base;
  20. digits++;
  21. }
  22.  
  23. start=((integer<0)? 1 : 0);
  24. a.resize(digits+start,' ');//allocate memory only once
  25. if (integer<0) a[0]='-';
  26.  
  27. piece=((integer<0)? 0-integer : integer);
  28. for(int i=0; piece > 0; i++ )
  29. {
  30. a[ digits+start-i-1] = (piece%base)+48;
  31. piece-= (piece%base);
  32. piece/=base;
  33. }
  34.  
  35. return a;
  36. }
  37.  
  38.  
  39. int main(int argc, char*argv[])
  40. {
  41. cout << itoa(55) << endl; //defaults to base 10
  42. cout << itoa(0,2) << endl;// 0
  43. cout << itoa(1,2) << endl;// 1
  44. cout << itoa(2,2) << endl;// 10
  45. cout << itoa(3,2) << endl;// 11
  46. cout << itoa(4,2) << endl;// 100
  47. cout << itoa(5,2) << endl;// 101
  48. cout << itoa(63,2) << endl;// 111111
  49. cout << itoa(-1,2) << endl;// -1
  50. cout << itoa(-2,2) << endl;// -10
  51. }
Last edited by blacklight332; Mar 3rd, 2009 at 10:46 pm.
Reputation Points: 21
Solved Threads: 0
Newbie Poster
blacklight332 is offline Offline
5 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ / MFC Date Validation
Next Thread in C++ Forum Timeline: Linked lists help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC