Thread: Char Help! "Packing " bits to a signle unsigned char

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    9
    Quote Originally Posted by itCbitC View Post
    An unsigned char can only hold values from 0-255 and you're trying to fit 01234567 into it.
    Thanks for the reply, I do understand that. Maybe this will further explain what i am trying to do:

    purpose of my function: the function prompts the user for input, reads 7 unsigned values representing data bits, and "packs" these bits into a single unsigned char


    What should returned: unsigned char that represents the 7 bits of data entered by the user. The MSB of the character is always 0. If the data entered by the user is d6 .. d0, the format of the byte returned is:
    MSB LSB
    0 d6 d5 d4 d3 d2 d2 d0

    Does this make any sense?
    Last edited by xxrexdartxx; 10-10-2009 at 12:06 PM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    One was using an array
    Can be done.
    Code:
    const int MAXBITS = 7;
    
    unsigned char ones_and_zeroes[MAXBITS  + 2] = "" ;  /*Holds 1'and zeroes*/
          /* one for '\0' and other for extra zero*/
         /*Now the input*/
         ....Make sure input has only 7 bits ,use fgets and don't forget to strip newline.
         /*After the input*/
         strrev(ones_and_zeroes);
         strcat(ones_and_zeroes, "0");
         strrev(ones_and_zeroes);
    Last edited by zalezog; 10-10-2009 at 12:59 PM.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    9
    Thank you.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    9
    Quote Originally Posted by zalezog View Post
    Can be done.
    Code:
    const int MAXBITS = 7;
    
    unsigned char ones_and_zeroes[MAXBITS  + 2] = "" ;  /*Holds 1'and zeroes*/
          /* one for '\0' and other for extra zero*/
         /*Now the input*/
         ....Make sure input has only 7 bits ,use fgets and don't forget to strip newline.
         /*After the input*/
         strrev(ones_and_zeroes);
         strcat(ones_and_zeroes, "0");
         strrev(ones_and_zeroes);

    Actually, this still does not return correctly(i think). When i return ones_and_zeroes i still get a
    warning: return makes integer from pointer without a cast

    Dosent this still return it one item at a time?

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Your function returns an unsigned char
    Code:
    unsigned char getMessageByte()
    But you are trying to return an unsigned char*. (ones_and_zeroes[] is an array)

    So one way would be:
    Code:
    void getMessage(unsigned char* ones_and_zeroes) {
         . . .
         . . .
    }
    .
    .
    int main(void) {
       /* memory allocated in main*/ 
        unsigned char ones_and_zeroes[MAXBITS  + 2] = "" ;
        .
        .
        getMessage(ones_and_zeroes);/*Function call now with a parameter*/
        .
        .
        return 0;
    }
    Last edited by zalezog; 10-11-2009 at 04:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 06:03 PM