PDA

View Full Version : Converting base 10 to base 2

MDMstudios
Apr 8, 2008, 06:30 PM
Hello, I am making a program that needs to have two ints, one of which is a decimal number the other is the binary form of that number, I have tried everything I can think of, but none of my ideas work, so I was wondering if any of you know how to do this. Thanks!

GimmeSlack12
Apr 8, 2008, 06:33 PM
http://people.csail.mit.edu/hammond/teaching/cs1001/base2.html
I haven't done this since I was in college. Needless to say I stick with my TI-85 for any of this stuff.

kainjow
Apr 8, 2008, 06:34 PM
Here is a little function I wrote a while ago that does just that:

int dec2bin(int num)
{
int bin = 0, k = 1;

while (num)
{
bin += (num % 2) * k;
k *= 10;
num /= 2;
}

return bin;
}

lee1210
Apr 8, 2008, 06:58 PM
kainjow's solution is interesting, but is only good for values of num less than 2^11 and greater than or equal to 0. Above 2^11 bin will overflow and unfortunate things will start happening. For negatives I'm not sure what the result will be, but I don't think it will be the 2s complement representation. The positive part is that you can print the result with %d and the number will "look" binary.

I'm not sure if this was precisely what you were after or not. It's certainly an interpretation of your original question. I had a different one (that you just want to display an int in its binary representation). Since integers are stored in binary anyway and you're using a binary computer, you can take advantage of that and just print based on each bit being set. This was what I came up with. it may not be the most optimal, but should work.

#include <stdio.h>
void printbinary(int);
int main(int argc, char *argv[]) {
int number=20343;
printbinary(number);
}

void printbinary(int number) {
int x;
for(x = 31;x>=0;x--) {
if((number & (1 << x)) != 0) {
printf("1");
} else {
printf("0");
}
}
printf("\n");
}

This could be adapted to store to a char[33] for later use pretty easily.

-Lee

P.S. I guess it says something about kainjow's mindset and my own, being that we just assumed you wanted C/C++/Obj-C, mine leaning towards C than C++ due to the printf rather than cout. If you are trying to do this in a language that isn't easily adapted to from C, let us know.

P.P.S. This assumes 32-bit integers. that might be a bad assumption. 8*sizeof(int) could be substituted as needed for the bit-width of an int.

MDMstudios
Apr 9, 2008, 09:50 PM
Thanks yall, I finally got something working that can do triple digit numbers.

#include <stdio.h>

int main (void) {

unsigned long int num;
long double tmpNum = .1;
int dec;
printf("Type in the decimal number you want to convert\n");
scanf("%i", &dec);

while ( dec != 0 )
{
if (dec % 2 == 1)
{
tmpNum *= 10;
num += tmpNum;
}

else
tmpNum *= 10;

dec = dec / 2;
}
printf("%i", num);

return 0;
}

ChrisA
Apr 9, 2008, 11:38 PM
Hello, I am making a program that needs to have two ints, one of which is a decimal number the other is the binary form of that number, I have tried everything I can think of, but none of my ideas work, so I was wondering if any of you know how to do this. Thanks!

I think the simplest solution is this...

Lets say "d" is the "normal integer" and "b is is to be the binary one.
It is just one line: b+= 10^n*(bit number n of d) for
all vales of n.

jeremy.king
Apr 10, 2008, 09:55 AM
Integer.toBinaryString(number);

where number is an int

HiRez
Apr 10, 2008, 11:26 AM
Here is one I wrote a long time ago that prints up to 64-bit with some ASCII borders:
void showBinary(long long input) {
int i = 0;
unsigned mask = 0;
printf("+--------+--------+--------+--------+--------+--------+--------+--------+\n");
for (i = 0; i < 64; i++) {
if (i % 8 == 0) {
printf("|");
}
mask = 0x1 << (63 - i);
if (input & mask) {
printf("1");
} else {
printf("0");
}
}
printf("|\n");
printf("+--------+--------+--------+--------+--------+--------+--------+--------+\n");
}

EDIT: Oops, I missed the part about "decimal". When you say that, you do mean an integer value, right? Not a float type? Well, I'll leave this here anyway.

AlmostThere
Apr 10, 2008, 12:25 PM
The c++ way to do this is to use a bitset:

#include <iostream>
#include <bitset>

using namespace std;

int main()
{
cout << bitset<16>(123) << endl;
cout << bitset<16>(string("101100101")).to_ulong() << endl;
}

NB. Bitset takes an unsigned integral template parameter - rather than 16 as here, which just seems clearer, you could use the appropriate number of digits from std::numeric_limits e.g. numeric_limits<unsigned long>::digits.

Output is:

0000000001111011
357