Thread: Char to Binary (ASCII to Bin)

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184

    Char to Binary (ASCII to Bin)

    Code:
    unsigned char *CharToBit (char pValue){
    
    	unsigned char *cByte;
    	unsigned int iCharASCII, iBit = 0;
    	int iCont;
    	
    	iCharASCII = pValue;
    	
    	for (iCont=7; iCont>=0; iCont--){
    		iBit = iCharASCII % 2;
    		iCharASCII = iCharASCII / 2;
    		
    		if (iBit == 1){
    			cByte [iCont] = '1';
    		}
    		else { cByte [iCont] = '0'; }
    	}
    	cByte [8] = '\0';
    	
    	return cByte;
    }
    
    ..........
    
    cFlag = substr (cStatus, 1,1);
    cFuncRes = cFlag [0];	// + '\0';
    cFlag = CharToBit (cFuncRes);
    This code generate an exception with unknow code.
    is There an specific function to convert a Char ASCII to Binary ?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Do you think you have the storage for the 8 binary characters?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Yes, I Need Manipulate the binary as 8 char variable.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    No, you haven't allocated storage for the 8 binary characters and that's why the exception.
    If you just want to display how an int looks in 1s and 0s consider a recursive function.
    Last edited by itCbitC; 11-18-2008 at 11:50 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by itCbitC
    If you just want to display how an int looks in 1s and 0s consider a recursive function.
    Nah, iteration is fine.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Please note that CharToBit is returning a pointer to char! So how have you declared/initialized cFlag?

    The function, itself, appears to looks sound.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    Nah, iteration is fine.
    cool beans

  8. #8
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Code:
    char *substr(const char *pstr, int start, int numchars) 
    {
    	char *pnew = malloc(numchars+1);
    	
    	strncpy(pnew, pstr + start, numchars);
    	pnew[numchars] = '\0';
    	return pnew;
    }
    Because strsub return a unsigned char *;

    Do you Know someone function to Convert Int to Binary as Char ?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sergioms
    Do you Know someone function to Convert Int to Binary as Char ?
    Basically, given a char, you wish to store its binary representation in a null terminated string?

    The main problem at this point is that this string is just a pointer that points to garbage. It looks like you want to use dynamic memory allocation with malloc() to create an array of 9 chars, and then fill the first 8 chars with the binary representation of the input char, and then terminate the string with a null character. Alternatively, you can avoid unnecessary dynamic memory allocation by having the caller pass a long enough string to the function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    [...]Alternatively, you can avoid unnecessary dynamic memory allocation by having the caller pass a long enough string to the function.
    and if the program is not multithreaded, a static 9 byte array, handled by the function, seems reasonable too.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Ok, thank's for all.

    I has used itoa function:

    itoa (number, string, 2);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Independent-Robert Fisk-Osama
    By a muslim in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-18-2001, 08:41 PM