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?
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?
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...
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
or this:
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 exampleCode:int main() { unsigned char x; scanf("%d",&x); do printf("%d",x%2?1:0); while (x=x>>1); }
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.
Hasn't this been answered a slew of times already?
-PreludeCode:#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; }
My best code is written with the delete key.
Maybe you can give some comments to it:
klausiCode:#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++; } }
Last edited by klausi; 03-14-2002 at 01:17 PM.
When I close my eyes nobody can see me...