[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


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

<< Back to: comp.lang.c Answers to Frequently Asked Questions (FAQ List)

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: Vote
how to convert a certain number to binary in C programming?


Answer by madhavi
Submitted on 8/4/2003
Rating:  Rate this answer: Vote
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: Vote
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: Vote
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: Vote
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: Vote
# include <stdio.h>
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: Vote
#include <stdio.h>
#include <conio.h>
#include <conio.c>

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: Vote
#include <stdio.h>
#include <conio.h>
#include <conio.c>

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: Vote
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: Vote
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: Vote
Here is a bloated example.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

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: Vote
#include<stdio.h>


    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: Vote
/* 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: Vote

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: Vote
# include <stdio.h>

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: Vote
# include <stdio.h>

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: Vote
how to convert 137.1 into binary numbers

 

Answer by Thorsten
Submitted on 1/10/2007
Rating: Not yet rated Rate this answer: Vote
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 Fil
Submitted on 3/20/2007
Rating: Not yet rated Rate this answer: Vote
// this will convert IP address, subnet mask
// and network address to a binary form
// so you can check if IP is valid part of
// network
//
// tnx to man above for original code :)

#include <conio.h>
#include <math.h>
#include <pcap.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <winsock.h>
#include <winnt.h>





void bin(int,int,int,int,int);

int bin_pole_ip[32];
int bin_pole_maska[32];
int bin_pole_siet[32];

void main (void) {

//printf ("Enter the no: to be converted into binary\n");
//scanf ("%d",&num);

bin(192,168,50,5,0);
bin(255,255,255,0,1);
bin(192,168,50,0,2);

   printf("\nIP:    ");
   for(int b_t = 0; b_t < 32; b_t++) {
      
      if(b_t%8 == 0) printf(" ");
      printf("%d",bin_pole_ip[b_t]);

   }

   printf("\nMaska: ");
   for(b_t = 0; b_t < 32; b_t++) {

      if(b_t%8 == 0) printf(" ");
      printf("%d",bin_pole_maska[b_t]);

   }
   
   printf("\nSiet:  ");
   for(b_t = 0; b_t < 32; b_t++) {

      if(b_t%8 == 0) printf(" ");
      printf("%d",bin_pole_siet[b_t]);

   }

   printf("\n\n");

}

void bin(int num,int num2,int num3,int num4,int kam) {

   int counter = 7;

   while (num != 0) {
   
      if(kam == 0) bin_pole_ip[counter] = num % 2;
      if(kam == 1) bin_pole_maska[counter] = num % 2;
      if(kam == 2) bin_pole_siet[counter] = num % 2;
      counter--;
      
      num = num /2;
   }

   counter = 15;

   while (num2 != 0) {
   
      if(kam == 0) bin_pole_ip[counter] = num2 % 2;
      if(kam == 1) bin_pole_maska[counter] = num2 % 2;
      if(kam == 2) bin_pole_siet[counter] = num2 % 2;
      counter--;
      
      num2 = num2 /2;
   }

   counter = 23;

   while (num3 != 0) {
   
      if(kam == 0) bin_pole_ip[counter] = num3 % 2;
      if(kam == 1) bin_pole_maska[counter] = num3 % 2;
      if(kam == 2) bin_pole_siet[counter] = num3 % 2;
      counter--;
      
      num3 = num3 /2;
   }

   counter = 31;

   while (num4 != 0) {
   
      if(kam == 0) bin_pole_ip[counter] = num4 % 2;
      if(kam == 1) bin_pole_maska[counter] = num4 % 2;
      if(kam == 2) bin_pole_siet[counter] = num4 % 2;
      counter--;
      
      num4 = num4 /2;
   }

   


   printf("\n");

}

 

Answer by Gianpa
Submitted on 4/28/2007
Rating: Not yet rated Rate this answer: Vote
#include <stdio.h>

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: Vote
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.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

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.

 

<< Back to: comp.lang.c Answers to Frequently Asked Questions (FAQ List)


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.