| | |
Decimal to Binary Conversation stored into a char array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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)
char binary(int number) { int temp = 0, c = 0; char buffer; if (number % 2 == 0) { temp = number / 2; } else { number = number - 1; temp = number / 2; } if (temp % 2 == 0) { buffer = '0'; } else { buffer = '1'; } return buffer; } int main() { int num; cin >> num; while(num != 0) { cout << binary(num); if (num % 2 == 0) { num = num / 2; } else { num = num - 1; num = num / 2; } } return 0; } Can someone help me out??
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?
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
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?
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
just write your own itoa.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; string itoa(const int &integer,int base=10) { if (integer==0) return string("0"); string a; int start, digits, piece; //count digits digits=0; piece=((integer<0)? 0-integer : integer); while( piece > 0 ) { piece-= (piece%base); piece/=base; digits++; } start=((integer<0)? 1 : 0); a.resize(digits+start,' ');//allocate memory only once if (integer<0) a[0]='-'; piece=((integer<0)? 0-integer : integer); for(int i=0; piece > 0; i++ ) { a[ digits+start-i-1] = (piece%base)+48; piece-= (piece%base); piece/=base; } return a; } int main(int argc, char*argv[]) { cout << itoa(55) << endl; //defaults to base 10 cout << itoa(0,2) << endl;// 0 cout << itoa(1,2) << endl;// 1 cout << itoa(2,2) << endl;// 10 cout << itoa(3,2) << endl;// 11 cout << itoa(4,2) << endl;// 100 cout << itoa(5,2) << endl;// 101 cout << itoa(63,2) << endl;// 111111 cout << itoa(-1,2) << endl;// -1 cout << itoa(-2,2) << endl;// -10 }
Last edited by blacklight332; Mar 3rd, 2009 at 10:46 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: C++ / MFC Date Validation
- Next Thread: Linked lists help
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





