Search the Q&A Archives

# ...convert a certain number to binary in C programming?

 Question by am-am Submitted on 7/6/2003 Related FAQ: comp.lang.c Answers to Frequently Asked Questions (FAQ List) Rating: Rate this question: N/A Worst Weak OK Good Great how to convert a certain number to binary in C programming?

 Answer by madhavi Submitted on 8/4/2003 Rating: Rate this answer: N/A Worst Weak OK Good Great We can convert a number into binary by dividing it with 2 for eg: 48 is there 48/2=24 and reminder R = 0 24/2=12 and R=0 12/2=6 and R=0 6/2= 3 and R=0 3/2 = 1 and so R= 1 So from this we will take all the reminders and so the binary number for 48 is 10000

 Answer by Darkman_nofear Submitted on 10/21/2003 Rating: Rate this answer: N/A Worst Weak OK Good Great the concept is right but if you follow the bit weight in the binary system it will be (1,2,4,8,16,32,64,128,256,..etc) so by this mentioned answer is the binary number for 16. but 48 is 110000

 Answer by naidu_trk Submitted on 12/29/2003 Rating: Rate this answer: N/A Worst Weak OK Good Great There is no conversion to binary as such in  c programming u need to display it in the binary form . Here is the short function  to do that [ recursive implementation ] void bin(int i){/* start bin */                                                                                 int j=0;                                                                                 if(i!=0){   j=i;   bin(i>>1);   printf(" %d ",j&0x01);    }   }  /* end bin */

 Answer by Rahul B Submitted on 1/29/2004 Rating: Rate this answer: N/A Worst Weak OK Good Great Storing the binary representation of a number in a string, is this what you wist to achieve. Try using sprintf() with format strings.

 Answer by harish Submitted on 4/7/2004 Rating: Rate this answer: N/A Worst Weak OK Good Great # include void bin (int); int main (void) { unsigned int num; printf ("Enter the no: to be converted into binary\n"); scanf ("%d",&num); bin (num); } void bin (int num) { while (num != 0) {   printf ("%d",num % 2);   num = num /2; } }

 Answer by idiot_123 Submitted on 10/3/2004 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great #include #include #include int main() {     int input, bin_no[8]={0,0,0,0,0,0,0,0},i=7;               //do decimal to binary conversion          for(input=0; input<256; input++)     {         do         {             bin_no[i] = input % 2;             input = input / 2;             i--;          }while(input!=0);              for(i=0 ; i<8 ; i++)         {                   printf("%d", bin_no[i]);         }         textcolor(WHITE);         printf("\n");     }     system("pause");     return 0; }

 Answer by idiot_123 Submitted on 10/3/2004 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great #include #include #include int main() {     int input, bin_no[8]={0,0,0,0,0,0,0,0},i=7;               //do decimal to binary conversion          for(input=0; input<256; input++)     {         do         {             bin_no[i] = input % 2;             input = input / 2;             i--;          }while(input!=0);              for(i=0 ; i<8 ; i++)         {                   printf("%d", bin_no[i]);         }         textcolor(WHITE);         printf("\n");     }     system("pause");     return 0; }

 Answer by Jignesh makawana Submitted on 2/26/2005 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great We can convert a number into binary by dividing it with 2 for eg: 48 is there 48/2=24 and reminder R = 0 24/2=12 and R=0 12/2=6 and R=0 6/2= 3 and R=0 3/2 = 1 and so R= 1 So from this we will take all the reminders and so the binary number for 48 is 110000

 Answer by Mau'Dib Submitted on 4/3/2005 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great Darkman_nofear s answer is also partially incorrect. "...so by this mentioned answer is the binary number for 16. but 48 is 110000." Actually: 100000 (base 2) == 32 (base 10) //Not 16.   110000 (base 2) == 48 (base 10) //Correct. To convert binary to decimal and vice versa simply use this eg.: 110001 (b2) = ? (b10) 5  4  3  2  1  0  //Add each 2^n. 1  1  0  0  0  1  //Binary num.

 Answer by Alexander Submitted on 10/3/2005 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great Here is a bloated example. #include #include #include int main (int argc, char* argv[]) {    int total, power, result = 0;    total = atoi (argv[1]);    power = (int) (log ((double) total) / log (2));    for (int x = power; x >= 0; x--) {       result += (int) (pow (10, x) * int (total / pow (2, x)));       total %= (int) pow (2, x);    }    printf ("%d\n", result);    return 0; }

 Answer by mohan,moni_mvj99@yahoo.com Submitted on 12/15/2005 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great #include     void main()     {    int a[10];    int n,i,j;    printf("enter value");    scanf("%d",&n);    for(i=0;i<10;i++)    {        if(n==1)        {       a[i]=1;       break;        }        else        {       a[i]=n%2;       n=n/2;        }    }        for(j=0;j<=i;j++)        {              printf("%d",a[j]);        } }

 Answer by marizion Submitted on 2/9/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great /* this function takes the number to convert and a string to place the binary representation of that number in. */ void binstring(int num,char * output) { int count = 0; int i,e; char temp[50]; while(num != 0) {   if(num % 2 == 0)   {    temp[count++] = '0';   }   else   {    temp[count++] = '1';   }   num = num >> 1; } // you now have the binary number in temp    // but it is backwards,    // eg ( "star" backwards is "rats") for( i = (--count),e = 0; i >= 0; i--,e++) {   output[e] = temp[i]; } output[e] = '\0'; // we have just used to for loop to reverse // the letters of the string // eg ("rats" to "star") }

 Answer by a123321 Submitted on 2/14/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great quote***************************** the concept is right but if you follow the bit weight in the binary system it will be (1,2,4,8,16,32,64,128,256,..etc) so by this mentioned answer is the binary number for 16. but 48 is 110000 ************************************** the first answer given is incomplete, since u need to divide until nothing is left. for eg: 48 is there 48/2=24 and reminder R = 0 24/2=12 and R=0 12/2=6 and R=0 6/2= 3 and R=0 3/2 = 1 and so R= 1 1/2= 0 and so R=1 and the binary is 110000

 Answer by Arnaud Submitted on 11/20/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great # include printb(int num){      char str[8];      printf("%s",itoa(num, str, 2)); } void main(void){      printb(17); }

 Answer by Arnaud Submitted on 11/20/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great # include printb(int num){      char str[8];      printf("%s",itoa(num, str, 2)); } void main(void){      printb(17); }

 Answer by sart Submitted on 11/28/2006 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great how to convert 137.1 into binary numbers

 Answer by Thorsten Submitted on 1/10/2007 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great The previous answer gives the Binary output backwards.  Here is a function for converting to Binary properly: void convertToBinary() {    int num;    int binary = 0;    int place = 0;    printf("Converting to Binary\n");    printf("Enter an integer: ");    scanf("%d", &num);    while (num != 0)    {       binary = binary + (num%2 * pow(10, place));       num = num /2;       place = place + 1;    }    printf("The equivalent binary is %d\n", binary); }

 Answer by Gianpa Submitted on 4/28/2007 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great #include int main() { int number; printf("Enter a number to be converted into binary form: "); scanf("%d",&number); printf("\nThe converted number is: "); while (number) { printf("%d",number%2); number /= 2; }

 Answer by okumbi Submitted on 4/28/2007 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great how to conver the binary to get ip/tcp address

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.