Thread: decimal to binary

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    decimal to binary

    i wanna write a function to compute the binary equivalent of a decimal number between 2 to 10.
    this binary is to be stored in a six element array.

    how?
    using recursive?

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Talking

    Simple Enough....

    int array[6],num;
    void main()
    {
    printf("\nEnter the decimal number: ");
    scanf("%d",&num);
    dispdec(num);
    }

    dispdec(int nu)
    {
    switch(nu)
    {
    case 2:
    array[0]=0;
    array[1]=0;
    array[2]=0;
    array[3]=0;
    array[4]=1;
    array[5]=0;
    break;
    case 3:
    array[0]=0;
    array[1]=0;
    array[2]=0;
    array[3]=0;
    array[4]=1;
    array[5]=1;
    break;
    case 4:
    array[0]=0;
    array[1]=0;
    array[2]=0;
    array[3]=1;
    array[4]=0;
    array[5]=0;
    break;
    case 5:
    array[0]=0;
    array[1]=0;
    array[2]=0;
    array[3]=1;
    array[4]=0;
    array[5]=1;
    break;
    case 6:
    array[0]=0;
    array[1]=0;
    array[2]=0;
    array[3]=1;
    array[4]=1;
    array[5]=0;
    break;
    case 7:
    array[0]=0;
    array[1]=0;
    array[2]=0;
    array[3]=1;
    array[4]=1;
    array[5]=1;
    break;
    case 8:
    array[0]=0;
    array[1]=0;
    array[2]=1;
    array[3]=0;
    array[4]=0;
    array[5]=0;
    break;
    case 9:
    array[0]=0;
    array[1]=0;
    array[2]=1;
    array[3]=0;
    array[4]=0;
    array[5]=1;
    break;
    case 10:
    array[0]=0;
    array[1]=0;
    array[2]=1;
    array[3]=0;
    array[4]=1;
    array[5]=0;
    break;
    }
    }

    Enjoy...

  3. #3
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Or, if you don't want to look like a total newb:
    Code:
    #include <stdio.h>
    
    int main( int argc, char **argv )
    {
      int num;
      char binary[9];
      int i, x;
    
      if ( argc > 1 )
        num = atoi( argv[1] );
      else
      {
        printf( "no decimal specified on command line\n" );
        return (1);
      }
    
      for ( i=7, x=1; i > -1; i--, x *= 2 )
      {
        if ( num & x )
          binary[i] = '1';
        else
          binary[i] = '0';
      }
    
      printf( "%s\n", binary );
    }
    Jason Deckard

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    or this:
    Code:
    int main() {
    unsigned char x;
    scanf("%d",&x);
    do printf("%d",x%2?1:0); while (x=x>>1);
    }
    this does it in reverse order, and stops when the number's finished. ie: 0000 0001 would be 1, and 0001 1101 would be 10111. but it's an example
    recursively:
    Code:
    int printbinary(unsigned char);
    int main() {
    unsigned char x;
    scanf("%d",&x);
    printbinary(x);
    }
    int printbinary(unsigned char x) {
    if (!x) return 0;
    printf("%d",x%2?1:0);
    printbinary(x>>1);
    }
    Last edited by ygfperson; 03-12-2002 at 01:15 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Hasn't this been answered a slew of times already?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int c = 10, i, j;
      char bTemp[8], bFinal[8];
      for ( i = 7, j = 0; i >= 0; i--, j++ ) {
        bTemp[i] = !!( c & ( 1 << i ) ); 
        bTemp[i] = bTemp[i] % 10 + '0';
        bFinal[j] = bTemp[i];
      }
      for ( i = 0; i < 8; i++ ) putchar ( bFinal[i] );
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Maybe you can give some comments to it:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define PUFFER 256
    
    
    void asctobin(void); /* converts ASCII-characters into binary digits */
    void bintoasc(void); /* converts binary digits into ASCII characters */
    
    unsigned char bin[]={1,2,4,8,16,32,64,128};
    
    int main(void)
    {
    	char chose;
    
    	for(chose=0;chose<40;chose++,printf("\n")) ; /* clear screen */
    
    again:
    	printf("Enter \'A\' to convert ASCII to binary or \'B\' to convert binary into ASCII: ");
    	scanf("%c",&chose);
    	if(chose=='A' || chose=='a') asctobin();
    	else
    	{
    		if(chose=='B' || chose=='b') bintoasc();
    		else{
    			rewind(stdin); /* back to the begin of stdin; if more then one character was entered */
    			goto again;
    		} /* only accepts 'A','a','B' and 'b' as inputs */
    	}
    
    	return 0;
    }
    
    
    void asctobin(void)
    {
            char *string,c;
    	int count,len;
    
    	printf("Enter string (max length: 255 characters): ");
    	rewind(stdin); /* back to the begin of stdin */
    	fgets(string,PUFFER-1,stdin);
    
    	len=strlen(string);
    
    	for(;len>0;len--) /* as often as the value for the length of the inputed string */
    	{
    		c=*string; /* the character string points to at the moment */
    		for(count=7;count>=0;count--)
    		{        
    			if(c>=bin[count])
    			{
    				printf("1");
    				c-=bin[count];
    			}else
    			printf("0");
    		}
    		*string++;
    		printf(" ");
    	}
    }
    
    
    void bintoasc(void)
    {
    	char *string,c;
    	int count=0,len;
    
    
    	printf("Enter digits (max 255):");
    	rewind(stdin); /* back to the begin of stdin */
    	fgets(string,PUFFER-1,stdin);
    
    	len=strlen(string);
    
    	for(;len>0;len--) /* as often as the value for the length of the inputed string */
    	{
    		if(*string=='1')
    		{
    			c+=bin[7-count];
    			count++;
    		}else 
    		if(*string=='0') count++;
    
    		if(count%8==0)
    		{
    			printf("%c",c);
    			count=c=0;
    		}
    
    		*string++;
    	}
    }
    klausi
    Last edited by klausi; 03-14-2002 at 01:17 PM.
    When I close my eyes nobody can see me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 08:35 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM