Thread: Printing a Character from Binary

  1. #1
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77

    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?!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    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
    When I close my eyes nobody can see me...

  4. #4
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    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?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    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
    When I close my eyes nobody can see me...

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Could you occassionally give some comments about my programming style?
    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
    When I close my eyes nobody can see me...

  7. #7
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character problem!!
    By cBegginer in forum C Programming
    Replies: 3
    Last Post: 09-02-2005, 11:51 PM
  2. Replies: 20
    Last Post: 08-21-2005, 07:49 PM
  3. Printing Hex as Binary
    By Mystic_Skies in forum C Programming
    Replies: 6
    Last Post: 11-22-2004, 04:18 PM
  4. printing strings (was similar problem)
    By weirdbeardmt in forum C Programming
    Replies: 5
    Last Post: 06-01-2004, 01:12 PM
  5. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM