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

This is a discussion on Char Help! "Packing " bits to a signle unsigned char within the C Programming forums, part of the General Programming Boards category; Hello everyone, i am new here and i am looking for a little help. What i am working on is ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    9

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

    Hello everyone, i am new here and i am looking for a little help. What i am working on is a function that will require 7 numbers inputted from a user. Then it will add a '0' before the items. Then it will return all items as a single unsigned char. For example, if the user enters: 1, 2, 3, 4, 5, 6, 7. It should return 01234567 as a single unsigned character.

    I am trying to do this two ways. One was using an array. The problem is that i cannot find how to return it as a single char. I know i cannot return the array because the function will just return one item at a time. Can anyone provide some help of examples how i can accomplish this? What i have is:
    Code:
    unsigned char getMessageByte() {
    unsigned char items[7];
    char zero[8] = "0";
    printf("Enter a 7 digit char of 1's and 0's \n");
    gets(items);
    //sets the parity bit
    strcat(zero, items);
    printf("%s\n",zero);
      return zero;
    }
    I have also tried this as accepting 7 different char as seperate variables. However, once again i do not understand how to combine those into a single unsigned char. That example is:
    Code:
    unsigned char getMessageByte() {
    unsigned char item0 = '0';
    unsigned char item1;
    unsigned char item2;
    unsigned char item3;
    unsigned char item4;
    unsigned char item5;
    unsigned char item6;
    unsigned char item7;
    unsigned char total;
    printf("Enter a string of 7 items consisting of only 1's and 0's: ");
    scanf("%c%c%c%c%c%c%c",&item1,&item2,&item3,&item4,&item5,&item6,&item7);
    
    //How do you combine item0+items1-7 to total???"
    return total;
    }
    Thanks in advance

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    An unsigned char can only hold values from 0-255 and you're trying to fit 01234567 into it.

  3. #3
    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 01:06 PM.

  4. #4
    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 01:59 PM.

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

  6. #6
    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?

  7. #7
    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 05:44 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    None of that is really packing numbers into a single unsigned char. You aren't really explaining exactly how you think it's going to pack it. What are your number supposed to mean? Like if I say "1235", what is that? Set bits 1, 2, 3 and 5? Or what exactly are you trying to pack? Add 1 + 2 +3 + 5? You're not clear on what your "packing" is really supposed to be doing.


    Quzah.
    Hope is the first step on the road to disappointment.

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, 02:01 AM
  3. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 04: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

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