Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
heyyy!!!!.... i need help to make a program to convert decimal numbers (base10) to base 2. i don´t know to start helpp plzzz!!!

well,
first, more clearly define your problem.
do you simply want a prompt that converts a number in to binary out, or do you want a file in to file out? Then i would suggest looking at std::out (at least in c++)
btw, what's this for?
jag mafer

well i need to convert a decimal number to a binary......... so far i have this code i dunno know if this is ok
#include
#include
#include
int main()
{
int numerator,counter,denomenator=2,cantidad=0;
int remainder=0,start;
char stringvalue[256];
memset(stringvalue,NULL,sizeof(stringvalue));
printf("Introduzca el numero");
scanf("%d",&numerator);
start=numerator;
counter=0;
do{
remainder=numerator%denomenator;
if (remainder==1)
stringvalue[counter]=1;
else
stringvalue[counter]=0;
cantidad=numerator/denomenator;
numerator=cantidad;
counter++;
}while(cantidad!=0);
strrev(stringvalue);
printf("Su numero %d en Binario es: %d",start,stringvalue);
return 0;
}check it out

THe computer stores teh numbers a binary. printf should have a switch that displays the number in hex.
instead of %d us %x (I think that is the switch)

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaHello again! This little program should do the trick.
#include
int main()
{
int *modulus;
int interger, bit = 0;
cout > interger;while(interger != 0) {
*(modulus + bit++) = interger % 2;
interger = interger / 2;
}
bit--;
while(bit >= 0)
cout You can use a simple pointer to store you bits in memory and a loop for dividing and obtaining modulus. Keep experimenting and best wishes.

sorry, to clear that up:
#include"iostream.h"
int main()
{
int *modulus;
int interger, bit = 0;
cout > interger;
while(interger != 0) {
*(modulus + bit++) = interger % 2;
interger = interger / 2;
}
bit--;
while(bit >= 0)
cout << *(modulus + bit--);
return 0;
}

Ok here goes again. Don't pay attention to the previous two:
#include"iostream.h"
int main()
{
int *modulus;
int interger, bit = 0;
cout > interger;
while(interger != 0) {
*(modulus + bit++) = interger % 2;
interger = interger / 2;
}
bit--;
while(bit >= 0)
cout << *(modulus + bit--);
return 0;
}

#include
{
int *modulus;
int interger, bit = 0;
cout > interger;
while(interger != 0) {
*(modulus + bit++) = interger % 2;
interger = interger / 2;
}
bit--;
while(bit >= 0)
cout << *(modulus + bit--);
return 0;
}

This is my last try.#includeint main(){int *modulus;int interger, bit = 0;cout > interger;while(interger != 0) {*(modulus + bit++) = interger % 2;interger = interger / 2;}bit--;while(bit >= 0)cout

Run this program with the deciaml number as the argument. It converts it to an unsigned long int. It is separated into to nibbles to distinguish the bits easily.
No need for the overhead of C++ here.
#include
#includeint main(int argc, char **argv)
{
int c;
unsigned int val;if (argc > 1) {
for (c = 0; *(*(argv + 1) + c); c++)
if ((*(*(argv + 1) + c) '9')) {
printf("%s ain't decimal, Honey\n", *(argv + 1));
return (0);
}
printf("%u decimal = ", val=(unsigned int)atol(*(argv + 1)));
for (c = 16; c > 0;) {
printf("%d", (val >> (--c)) & 1);
if (!(c % 4))
printf(" ");
}
printf(" binary\n");
}
return (0);
}Mr. A

This website doesn't take cut & paste literally.
Let's see if it works this time...
#include
#includeint main(int argc, char **argv)
{
int c;
unsigned int val;if (argc > 1) {
for (c = 0; *(*(argv + 1) + c); c++)
if ((*(*(argv + 1) + c) 57)) {
printf("%s ain't decimal, Honey\n", *(argv + 1));
return (0);
}
printf("%u decimal = ", val=(unsigned int)atol(*(argv + 1)));
for (c = 16; c > 0;) {
printf("%d", (val >> (--c)) & 1);
if (!(c % 4))
printf(" ");
}
printf(" binary\n");
}
return (0);
}

If this doesn't work, I'm giving up.
I don't the the html codes for the vertical bars and this just isn't working...
#include
#includeint main(int argc, char **argv)
{
int c;
unsigned int val;if (argc > 1) {
for (c = 0; *(*(argv + 1) + c); c++)
if ((*(*(argv + 1) + c) 57)) {
printf("%s ain't decimal, Honey\n", *(argv + 1));
return (0);
}
printf("%u decimal = ", val=(unsigned int)atol(*(argv + 1)));
for (c = 16; c > 0;) {
printf("%d", (val >> (--c)) & 1);
if (!(c % 4))
printf(" ");
}
printf(" binary\n");
}
return (0);
}

Hi,
itoa() does it, the 3rd parameter given 2, converts the number to a "stream" of 1's and 0's. The converted value is in a string format(char[], i mean array of chars.). U can use this is u want to just print the number in 1's and 0's, but if u want to do any arthimatic operations on it, Trust me, use the value as hex and do it. it will work out for sure.
I trust this help us out.Venkat.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |