English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i want to input decimal, and would like the program to output binary numbers. i want to know if there is a function for this, or a program. pls can you guys lead me in the right direction. As in if i input "19", i want the program to output stuff like "10011"

2007-02-22 13:04:31 · 6 answers · asked by yaro maiwayo 2 in Computers & Internet Programming & Design

help pls

2007-02-22 15:12:55 · update #1

6 answers

I would hope "so far north", "BigRez", and "Scott" are programmers I never have to work with. It's not uncommon to see green programmers attempt to do base conversion on their own. First, it's not necessary, as this is implemented in the C/C++ standard library. Second, usually such attempts are pretty crude and examples of poor code.

There's two ways to go about this: the C way and the C++ way. I'll show examples of both, and hopefully you can head over to Google and explore on your own (You can go to google.com, right?).

C:
Assuming you have a buffer you filled with fgets or something.
#include
char *inputstr; //populated with input
long l;
l = strtol(inputstr, NULL, 2); //l contains the binary value of the decimal number in inputstr

C++:
There's no standard way. Options are to roll or your own method, use C functions, or use bitsets. Take a look at the USENET question and answers: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/97589b73e45511fd/738f8787e80b5963%23738f8787e80b5963

2007-02-22 15:17:53 · answer #1 · answered by csanon 6 · 0 0

placed the numbers right into a string, then opposite the string. in case you do not % to debris with strings, you will could use protecting and bit moving edit: if it is for a classification, you are able to probable stick to the string concepts-set as its what you have been taught. If it quite is a private undertaking, you are able to attempt this. it's going to print out the great binary fee (consisting of foremost 0's) for any integer sort (something from char to long long, signed or unsigned) in the appropriate order. #contain int significant() { int dec_num, reminder, mask; printf("enter the decimal sort: "); scanf("%d",&dec_num); mask=(0x1<<(sizeof(dec_num)*8)-a million); for(int i = 0; i < 8*sizeof(dec_num); i++) { if((dec_num&mask) != 0) printf("a million"); else printf("0"); dec_num=dec_num << a million; } return 0; }

2016-12-17 16:41:16 · answer #2 · answered by ? 4 · 0 0

i saw an example of a recursive function for printing a number in any base in a textbook but i dont remember where, eziest way to do but probly not best wud be with bit operators

int a; char *b
for(i=0;i<=[sizeof(int)*8]-1;i++)
{
b[i]< a>>=1;
}
cout<

2007-02-22 13:42:47 · answer #3 · answered by Anonymous · 0 0

A couple of ways...

First, using the itoa() function...

char buffer[33];
...
itoa(integer,buffer,2)
cout << buffer << endl;

Here's another way with full binary representation:

int myVal=12;

int main(int argc, char* argv[])
{
binaryPrint(myVal);
return 0;
}

void binaryPrintByte(int x)
{
for(int n=0; n<8; n++)
{
cout << ((x & 0x80) ? "1" : "0" );
if (n==3)
cout << " ";
x = x<<1;
}
}

void binaryPrint(int x)
{
int hi, lo;
hi=(x>>8) & 0xff;
lo=x&0xff;
binaryPrintByte(hi);
cout << " ";
binaryPrintByte(lo);
cout << endl;
}

2007-02-22 13:40:06 · answer #4 · answered by BigRez 6 · 0 0

Well, I can suggest something like this:
do
{
a[i]=dec%2;
dec=dec/2;
i++;
}while(dec!=0);
for(n=i-1;n>=0;n--)
printf("%d",a[n]);

2007-02-22 13:13:16 · answer #5 · answered by so far north 3 · 0 0

windows calculator does this, and has for a very long time.

2007-02-22 13:12:40 · answer #6 · answered by D 4 · 0 0

fedest.com, questions and answers