Printing a Character from Binary

• 02-18-2002
SavesTheDay
Printing a Character from Binary
Is there anyway to print a character which you have BINARY for? Such as:

Code:

``` char Binary[]="10010110"; int x; x=atoi(Binary); printf("??");```
I have the binary digits store in a character array...then I use atoi to put them into an int array...but when I try to display using %c, it displays the character of "10010110"....it uses that as the actual number...not the number in binary.....Anyone?!
• 02-18-2002
QuestionC
As far as I know, there isn't any standard function to handle this. The code for such a function, however, isn't too terribly complicated...
Code:

```int atob (const char * intMe) {  int result = 0;  int i;  for (i = 0; intMe[i] != '\0'; i++)  {   result <<= 1; // Shift the values in result left once.  Same ase     // result *= 2;   // if intMe[i] == '0', we don't do anything.   if (intMe[i] == '1')   {   result += 1;   }  }  return result; }```
• 02-18-2002
klausi
Check this!
If you e-mail me I could send some source code to you Iīm writing at the moment which does exactly the same.
Because I have hardly time for programming at the moment, it will last about a week until I have finished it.

klausi
• 02-18-2002
SavesTheDay
I made one during Chemistry class...it's pretty simple...goes like this:

Code:

```char FullBinary[9]="10101100"; int Binary[8]={128,64,32,16,8,4,2,1}; int x,Total;   for (x=0; x<=8; x++);       if (FullBinary[x]==1)  {            Total = Total+Binary[x];            x++;    }       if (FullBinary[x]==0)  {           x++;                       }     }  printf("%c",Total);```
See?
• 02-19-2002
klausi
Here is my solution now. Finished it yesterday:
Code:

```#include <stdio.h> #include <stdlib.h> /* for allocating memory for the strings */ #define PUFFER 256 int lenof(char *str); /* returns the length of a string inclusive the ending '\0' */ 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}; /* bitfield */ int main(void) {         char chose;         for(chose=0;chose<50;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;         if((string=(char*)malloc(PUFFER))==NULL)         {                 printf("Cannot allocate enough memory!");                 exit(1); /* exit with the return code an error */         }         printf("Enter string (max length: 255 characters): ");         rewind(stdin); /* back to the begin of stdin */         fgets(string,PUFFER-1,stdin);         len=lenof(string);         for(;len>1;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(" ");         } } int lenof(char *str) {         int count=0;         while(*str!='\0' && count<256)         {                 *str++;                 count++;         }         return(count); } void bintoasc(void) {         char *string,c;         int count=0,len;         if((string=(char*)malloc(PUFFER))==NULL)         {                 printf("Cannot allocate enough memory!");                 exit(1); /* exit with the return code for an error */         }         printf("Enter digits (max 255):");         rewind(stdin); /* back to the begin of stdin */         fgets(string,PUFFER-1,stdin);         len=lenof(string);         for(;len>1;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
• 02-19-2002
klausi
Itīs actually my second program I wrote in C.
Maybe you can also give some better comments than I wrote into the program.
I programmed the function lenof() on myself, because I think itīs more efficient than including the string library.

klausi
• 02-19-2002
alex
Hi!

There is a library function called strtol...
Code:

```... #include <stdlib.h> ... char Binary[]="10010110"; int x; ... x=strtol(Binary, (char **)NULL, 2); printf("%i\n", x); ...```
alex