Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

O_Joe

decimal to binary conversion

This topic is 5351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i was wandering if any1 had some simple code for converting decimal number into binary thanx

Share this post


Link to post
Share on other sites
Advertisement
Is this a homework question? Because we all love answering to those kinds of questions.

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Anonymous Poster
If you want to go higher, just keep adding lines to the switch.


ROFL!

--cfmdobbie

Share this post


Link to post
Share on other sites
Guest 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;
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!