Decimal to Binary Conversation stored into a char array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 66
Reputation: djextreme5 is on a distinguished road 
Solved Threads: 4
djextreme5's Avatar
djextreme5 djextreme5 is offline Offline
Junior Poster in Training

Decimal to Binary Conversation stored into a char array

 
0
  #1
Mar 3rd, 2009
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.

  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??
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: rgfirefly24 is an unknown quantity at this point 
Solved Threads: 0
rgfirefly24 rgfirefly24 is offline Offline
Newbie Poster

Re: Decimal to Binary Conversation stored into a char array

 
0
  #2
Mar 3rd, 2009
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)
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 66
Reputation: djextreme5 is on a distinguished road 
Solved Threads: 4
djextreme5's Avatar
djextreme5 djextreme5 is offline Offline
Junior Poster in Training

Re: Decimal to Binary Conversation stored into a char array

 
0
  #3
Mar 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 941
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Decimal to Binary Conversation stored into a char array

 
0
  #4
Mar 3rd, 2009
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?
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 66
Reputation: djextreme5 is on a distinguished road 
Solved Threads: 4
djextreme5's Avatar
djextreme5 djextreme5 is offline Offline
Junior Poster in Training

Re: Decimal to Binary Conversation stored into a char array

 
0
  #5
Mar 3rd, 2009
Originally Posted by MosaicFuneral View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 941
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Decimal to Binary Conversation stored into a char array

 
0
  #6
Mar 3rd, 2009
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.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: blacklight332 is an unknown quantity at this point 
Solved Threads: 0
blacklight332 blacklight332 is offline Offline
Newbie Poster

Re: Decimal to Binary Conversation stored into a char array

 
0
  #7
Mar 3rd, 2009
just write your own itoa.
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC