Thread: Unsigned char array to unsigned char?

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    12

    Unsigned char array to unsigned char?

    Hello everyone,

    I am having trouble with converting an char array to a char, both of the unsigned type. I actually have no idea how to do it.
    Here is some pseudocode for what I'm trying to do:
    Code:
    main()
    {
          unsigned char ucMsgData[6];
          unsigned char ucData;
    
          ucData=Convf(ucMsgData);
    }
    
    unsigned char Convf(unsigned char array)
    {
          /*Conversion*/
    }

    Many thanks in advance guys

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    How do you plan to convert a char array to an unsigned char? What is the algorithm that you have in mind?
    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

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Define Convf() as
    Code:
    unsigned char Convf(unsigned char *array)
    {
         unsigned char retval;
         /*   Do conversion based on array[0], array[1], array[2], array[3], array[4], and array[5].   Store result in retval */
         return retval;
    }
    Bear in mind that there is no way of stopping the code accessing array[26], even if it doesn't exist. It is your responsibility to ensure such things don't happen.

    Out of curiosity, let's say you have an array of unsigned char containing the characters "MadCow". What would you expect the result of the conversion to be?


    Also, it is good style to explicitly implement main() as returning int. And it is also a good idea to declare Convf() so it is seen by the compiler before main() [then placing the definition/implementation after main(), or even in a separate source file, is fine].
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    12
    Quote Originally Posted by grumpy View Post
    Define Convf() as
    Code:
    unsigned char Convf(unsigned char *array)
    {
         unsigned char retval;
         /*   Do conversion based on array[0], array[1], array[2], array[3], array[4], and array[5].   Store result in retval */
         return retval;
    }
    Bear in mind that there is no way of stopping the code accessing array[26], even if it doesn't exist. It is your responsibility to ensure such things don't happen.

    Out of curiosity, let's say you have an array of unsigned char containing the characters "MadCow". What would you expect the result of the conversion to be?


    Also, it is good style to explicitly implement main() as returning int. And it is also a good idea to declare Convf() so it is seen by the compiler before main() [then placing the definition/implementation after main(), or even in a separate source file, is fine].
    Thanks for the reply.

    Well I was thinking of something like this to ensure that the limit of the array would be right:
    Code:
    for(i=sizeof(ucMsgData);i>0;i--)
    {
         /*conversion*/
    }
    For the MadCow:
    Code:
    ucMsgData[0] = "M";
    ucMsgData[1] = "a";
    ucMsgData[2] = "d";
    ucMsgData[3] = "C";
    ucMsgData[4] = "o";
    ucMsgData[5] = "w"; 
    
    ucData=Convf(ucMsgData); //Store MadCow in ucData
    My main function is returning an int, and the Convf() has been properly declared. Sorry for not mentioning it.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Oh, yikes.

    Within your function sizeof(MsgData) will give the size of a pointer, not the number of characters in the array. They are different things.

    If you want your function to use information about the size of the passed array, it is necessary to pass that informatiom separately (eg say as an additional argument "unsigned char Convf(unsigned char *array, int length_of_array)").

    There is also virtually no way to store six characters in one. You can derive some value (say a checksum) but that is not the same as putting all six values into one.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    12
    Hmmm they are indeed different things, stupid me.
    The MsgData is an array of numbers. I assume I can split the array an store the two different parts in in 2 unsigned integers?

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Kishintai View Post
    The MsgData is an array of numbers. I assume I can split the array an store the two different parts in in 2 unsigned integers?
    And why do you want to do that? What do the numbers in the array represent?

    (BTW:
    Code:
    char arr[6] = { 'M', 'a', 'd', 'C', 'o', 'w' }
    is also just an array of numbers)

    Bye, Andreas

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    12
    Quote Originally Posted by AndiPersti View Post
    And why do you want to do that? What do the numbers in the array represent?

    (BTW:
    Code:
    char arr[6] = { 'M', 'a', 'd', 'C', 'o', 'w' }
    is also just an array of numbers)

    Bye, Andreas
    Well to be complete. The array is respectively data from the CAN-network. This data will be then visualized on a LCD. But the data send to the LCD must be split into MSB and LSB.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Can you give an example of the data and how it should be sent to the LCD?

    Bye, Andreas

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    12
    Quote Originally Posted by AndiPersti View Post
    Can you give an example of the data and how it should be sent to the LCD?

    Bye, Andreas
    Of course. Btw I use Code Composer Studio with a Cortex M4 board.

    WRITE = 0x01
    DIGITS_SPEED = 0x0F
    DIGITS_OBJECT_ID = 0x00
    MSB = 0x00
    LSB = 0x0F

    So:
    Code:
    while(Received_Data!=ACK)
        {
             ROM_UARTCharPutNonBlocking(UART0_BASE,WRITE);                                                    // Write Cmd
             ROM_UARTCharPutNonBlocking(UART0_BASE,DIGITS);                                                    // Object Type
              ROM_UARTCharPutNonBlocking(UART0_BASE,DIGITS_OBJECT_ID);                                        // Object ID
                ROM_UARTCharPutNonBlocking(UART0_BASE,MSB_CMD);                                                    // MSB Data
                ROM_UARTCharPutNonBlocking(UART0_BASE,LSB_CMD);                                                 // LSB Data
             // Calculate Checksum
             CHECKSUM=WRITE^DIGITS^DIGITS_OBJECT_ID^MSB_CMD^LSB_CMD;
             ROM_UARTCharPutNonBlocking(UART0_BASE,CHECKSUM);                                                // Checksum
    Received_Data = UARTCharGet(UART0_BASE);
        }
    Write is of course a write cmd to the lcd. DIGITS and DIGITS_OBJECT_ID are the declarations of an element of the lcd. In this case a simple a screen with 3 digits. From the 5 cmd I calculate the checksum.
    Everything works fine: I can read and write to the LCD. I can read and write with ID-filtering to the CAN. My only problem is to convert the incoming data of the CAN to something that I can work with, so that I can send it to my LCD.
    I hope that you are understanding what I mean.
    Last edited by Kishintai; 04-26-2013 at 11:28 AM.

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    12
    Btw thanks guys for willing helping me out

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Can you summarize your requirements? So far, as I understand it:

    - You have an array of integers, each of which represents a command
    - For each element of this array, you want to:
    - Extract the high byte, and send it to the LCD
    - Extract the low byte, and send it to the LCD

    Is this near correct?

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    12
    Quote Originally Posted by Matticus View Post
    Can you summarize your requirements? So far, as I understand it:

    - You have an array of integers, each of which represents a command
    - For each element of this array, you want to:
    - Extract the high byte, and send it to the LCD
    - Extract the low byte, and send it to the LCD

    Is this near correct?
    I'll try to rephrase.

    - So I have an array of integers that is defined as an: unsigned char ucMsgData[6];
    - The values of that array must be stocked in: unsigned char ucData;

    Example:
    Code:
    unsigned char ucData = 0;
    unsigned char ucMsgData[6]={1,2,3,4,5,6};
    
    /*Insert the mystic function right here*/
    // Result:
    unsigned char ucData = 123456;                                 // Each value of the elements of the array must be stocked and joined in ucData
    - Once the data has been joined in ucData it will be splitted in MSB and LSB


    The splitting will not be a big problem. I'm more lost in time and space about joining the different values of the arrayelements in one variable.

    Again thanks guys with helping me out.
    Last edited by Kishintai; 04-26-2013 at 01:08 PM.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    How about an algorithm that adds the first number to a result variable, shifts that result over one place (i.e. multiply by the base), adds the next number, etc. This is assuming the result will fit in the data type you designate for the result (originally you had "unsigned char" but now you're showing "unsigned").

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    12
    Quote Originally Posted by Matticus View Post
    How about an algorithm that adds the first number to a result variable, shifts that result over one place (i.e. multiply by the base), adds the next number, etc. This is assuming the result will fit in the data type you designate for the result (originally you had "unsigned char" but now you're showing "unsigned").
    It's still an unsigned char, it wasn't very visible because of a typo.
    How do you mean multypling by the base? And for joining the next number, what function do you recommend me?
    I've never done something like that before0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  2. array<unsigned char>^ vs char[ ]
    By jlangfo5 in forum C++ Programming
    Replies: 6
    Last Post: 12-23-2011, 03:39 AM
  3. uincode char array to array<unsigned char,1>^
    By ripspinner in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2009, 05:41 PM
  4. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  5. Converting unsigned long array to unsigned char array
    By delvec28 in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:53 PM