convert char to binary???

This is a discussion on convert char to binary??? within the C Programming forums, part of the General Programming Boards category; Hi guys, Is there a standard lib func that convert a char to binary value?...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    1

    convert char to binary???

    Hi guys,

    Is there a standard lib func that convert a char to binary value?

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    A char is inherently a binary value, as are all variables.

    Code:
    printf("%c%c%c%c%c%c%c%c",
       (ch&0x80)?"1":"0",
       (ch&0x40)?"1":"0",
       (ch&0x20)?"1":"0",
       (ch&0x10)?"1":"0",
       (ch&0x08)?"1":"0",
       (ch&0x04)?"1":"0",
       (ch&0x02)?"1":"0",
       (ch&0x01)?"1":"0"
    );

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    In the above code, "1" and "0" should be '1' and '0', respectively, to match the format specifiers passed to printf.
    Last edited by Dave_Sinkula; 08-01-2003 at 12:55 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    Simply you can do this

    Hi,
    Either you use & operator or use standard logic to do this. I have made in standard logic. just call this func.
    Code:
    char*  chartobin(int c)
    {
    	static char binarr[16];
    	int i;
    	for(i=1;i<=15;i++)
    	{
    		binarr[i]=c%2;		
    		c=c/2;		
    	}
    	return binarr;
    }
    Saravanan.T.S.
    Beginner.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    char*  chartobin(int c)
    {
    	static char binarr[16];
    	int i;
    	for(i=1;i<=15;i++)
    	{
    		binarr[i]=c%2;		
    		c=c/2;		
    	}
    	/* null terminator required to make binarr a string */
    	return binarr;
    }
    An int is not necessarily 16 bits. And you would need an extra char for the null byte if you want binarr to be a string.

    In C, arrays are indexed starting from zero.

    The usual idiom is i < 16 (to correspond with the above code).
    Last edited by Dave_Sinkula; 08-01-2003 at 10:27 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    One way to "convert char to binary" might be as follows.
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    char *foo(char *dst, unsigned char value)
    {
       unsigned char bit;
       char *start = dst;
       for ( bit = 1 << (CHAR_BIT - 1); bit; bit >>= 1 )
       {
          *dst++ = value & bit ? '1' : '0';
       }
       *dst = '\0';
       return start;
    }
    
    int main(void)
    {
       unsigned char u = 117;
       char b [ CHAR_BIT * sizeof(u) + 1 ];
       printf("u = '%c' = 0%o = %d = 0x%X = \"%s\"\n", u, u, u, u, foo(b, u));
       return(0);
    }
    
    /* my output
    u = 'u' = 0165 = 117 = 0x75 = "01110101"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >A char is inherently a binary value, as are all variables.
    Yes, but a char is not always 8 bits.

    >just call this func.
    And this works for you? This may work better:
    Code:
    #include <limits.h>
    
    char *chartobin ( unsigned char c )
    {
        static char bin[CHAR_BIT + 1] = {0};
        int         i;
    
        for ( i = CHAR_BIT - 1; i >= 0; i-- )
        {
            bin[i] = (c % 2) + '0';
            c /= 2;
        }
    
        return bin;
    }
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    4
    im trying to get two input from user (enter character one) (enter charater two) then i would like to get it converted to binary then display the hamming distance between two characters how would i do that..

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,255
    Quote Originally Posted by saravana View Post
    im trying to get two input from user (enter character one) (enter charater two) then i would like to get it converted to binary then display the hamming distance between two characters how would i do that..
    You should create a new thread instead of bumping a thread that is 6 years old.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-22-2008, 08:20 PM
  2. convert unsigned char to char *
    By keeper in forum C++ Programming
    Replies: 2
    Last Post: 03-03-2006, 06:53 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 10:40 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21