Char to Binary (ASCII to Bin)

This is a discussion on Char to Binary (ASCII to Bin) within the C Programming forums, part of the General Programming Boards category; Code: unsigned char *CharToBit (char pValue){ unsigned char *cByte; unsigned int iCharASCII, iBit = 0; int iCont; iCharASCII = pValue; ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    158

    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,047
    Do you think you have the storage for the 8 binary characters?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    Yes, I Need Manipulate the binary as 8 char variable.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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 10:50 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    602
    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,047
    Quote Originally Posted by laserlight View Post
    Nah, iteration is fine.
    cool beans

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    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
    20,980
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    [...]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
    Posts
    158
    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, 11:36 AM
  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

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