decimal to binary conversion

Started by
20 comments, last by O_Joe 19 years, 12 months ago
i was wandering if any1 had some simple code for converting decimal number into binary thanx
Advertisement
Is this a homework question? Because we all love answering to those kinds of questions.
Well, R2D22U2..
Divide the decimal value by 2
Store the result
Divide the new decimal value by 2
Until the decimal value equals 0

e.g. 65 / 2 = 32 r 1
32 / 2 = 16 r 0
16 / 2 = 8 r 0
8 / 2 = 4 r 0
4 / 2 = 2 r 0
2 / 2 = 1 r 0
1 / 2 = 0 r 1

Therefore, binary value of 65 is 1000001

Hope that helps my man
I have a function written in C if you want it

What did you think of the Irish beating the English in rugby ?

Oh no, another one of these threads!

<a href="http://www.gamedev.net/community/forums/topic.asp?whichpage=1&pagesize=20&topic_id=212625">See if a number is odd</a>

Okay, let''s here it guys... your best implementation of converting decimal to binary!

Here''s mine: although it only works up to 10

void ConvertDecimalToBinary(int d, char *s) {   switch (d) {      case 0: strcpy(s, "0"); break;      case 1: strcpy(s, "1"); break;      case 2: strcpy(s, "10"); break;      case 3: strcpy(s, "11"); break;      case 4: strcpy(s, "100"); break;      case 5: strcpy(s, "101"); break;      case 6: strcpy(s, "110"); break;      case 7: strcpy(s, "111"); break;      case 8: strcpy(s, "1000"); break;      case 9: strcpy(s, "1001"); break;      case 10: strcpy(s, "1010"); break;   }}


If you want to go higher, just keep adding lines to the switch.
Stupid forum software, how do you get links to work?

http://www.gamedev.net/community/forums/topic.asp?whichpage=1&pagesize=20&topic_id=212625
I suppose you can''t, if you''re hiding behind a mask of anonymity.

See if a number is odd
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
quote:Original post by Anonymous Poster
If you want to go higher, just keep adding lines to the switch.


ROFL!

--cfmdobbie
here it is....
#include <iostream>#include <math.h>#include <string>using namespace std;string Num2Bin(int num){	string ret;	char tmp;	ret = "";	while( num > 0){		itoa((num%2),&tmp,10);		ret += tmp;		num /= 2;	}	return ret;}int main(){	int num;	cout << "Please input a number to convert to a binary string: ";	cin >> num;	cout << Num2Bin(num) << endl;	return 0;}
quote:Original post by Anonymous Poster
here it is....
#include <iostream>#include <math.h>#include <string>using namespace std;string Num2Bin(int num){	string ret;	char tmp;	ret = "";	while( num > 0){		itoa((num%2),&tmp,10);		ret += tmp;		num /= 2;	}	return ret;}int main(){	int num;	cout << "Please input a number to convert to a binary string: ";	cin >> num;	cout << Num2Bin(num) << endl;	return 0;}



As an alternative you could use the right shift operator and mask it which would avoid having to perform the division:

   string Num2Bin( int num ){    unsigned int mask     = 1;    int          bitcount = sizeof( int ) * 8;    char   tmp;    string ret;    ret = "";    for( int i = 0; i < bitcount; i++ )    {        itoa( num & mask, &tmp, 10 );        ret += tmp;        num >> 1;    }    return ret;}


I'm not sure how the string works, so this may give the binary string backwards, but I think that should work. Conversely, I suppose you could left shift the mask instead of right shifting the input value.

[edited by - simba on March 30, 2004 3:22:38 PM]
simba is correct, the string is reversed, here is the mod to reverse the string.
#include <iostream>#include <math.h>#include <string>using namespace std;string reverseStr(string str){	string ret;	int i = str.length();	int j = 0;	ret = str;	while(i--){		ret[j++] = str[i];	}	return ret;}string Num2Bin(int num){	string ret;	char tmp;	ret = "";	while( num > 0){		itoa((num%2),&tmp,10);		ret += tmp;		num >>= 1;  // devide num by 2.	}	//need to reverse the string because it is stored backwards.	return reverseStr(ret);}int main(){	int num;	cout << "Please input a number to convert to a binary string: ";	cin >> num;	cout << Num2Bin(num) << endl;	return 0;}

This topic is closed to new replies.

Advertisement