Thread: Casting an array problem from unsigned int to uint8_t

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    50

    Casting an array problem from unsigned int to uint8_t

    Hi guys, Im facing a serious problem and I need your help please!
    what Im facing is that Im getting value from another function as integer
    which they are :
    Code:
    {5,1,2,3,4,5}
    so those value exactly I want to copy them to an array type uint8_t.
    this means the array that I want to get is
    Code:
    uint8_t arr={5,1,2,3,4,5};
    what actually the problem?
    the problem is this if I want to initialize and array type uint8_t with those values then I write immediately in the compiler:
    Code:
    uint8_t arr={5,1,2,4,5};
    (I mean by intilizing immediately ..once I write my syntax uint8_t arr in my compiler I write immediately
    Code:
    {5,1,2,3,4,5}
    )
    the problem that those values Im getting them from other function ..and I want those values to be entered as it's in an array type uint8_t this means my array would be the same with those value but type uint8_t =>
    Code:
    uint8_t arr={5,1,2,3,4,5};
    how can I do that ? I tried to use unsigned int and starting filling it with values
    Code:
     {5,1,2,3,4,5}
    but it didn't work!

    any help how can I implement that? thanks alot.

    what I mean by code is this:
    Code:
    int array2={5,1,2,3,4,5};
    unit8_t array1[6]={0};
    for(int i=0;i<(sizeof(array2)/sizeof(array2[0]));i++)
    {
    array1[i]=array[i];} /*I want the same values of array2 to enter to array1 but the type of array to be uint8_t*/
    }
    so what I expect to get once I print output is the array1 which it should be :
    Code:
     {5,1,2,3,4,5}
    and array1 type uint8_t with the same values that I copied them to it.

    but the output is wrong answer, it's���a��™��I��

    any help please? thanks

    Last edited by Brian_Teir; 07-27-2020 at 11:07 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,942
    Maybe the problem lies with the printing of the output.

    I suggest that you post the smallest and simplest compilable program that demonstrates this problem.
    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
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,784
    Perhaps the problem is that
    Code:
    int array2={5,1,2,3,4,5};
    is not an array

    Code:
    int array2[]={5,1,2,3,4,5};
    might work better

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    927
    This is a bit much, but the following might help you to better understand whatever it is you're working with.

    Code:
    #include <cstdint>
    #include <vector>
    
    void arrays() {
        // this is an integer initialized to 7
        int a = 7;
    
        // this is an array of 4 integers uninitialized
        int b[4];
    
        // this is an array of 3 integers initialized to 1, 2, and 7
        int c[3] = {1, 2, a};
    
        // this is an array of 3 integers initialized to 1, 2, and 7
        int d[] = {1, 2, a};
    
        // this is an array of 4 integers initialized to 1, 2, 7, and 0
        int e[4] = {1, 2, a};
    
        // this is an array of 3 integers initialized to 0, 0, and 0
        int f[3] = {};
    
        // error: array must be initialized with a brace-enclosed initializer
        // int g[3] = c;
    
        // error: initializer fails to determine size of ‘h’
        // char h[] = e;
        
        // error: scalar object ‘i’ requires one element in initializer
        // int i = {1, 2, 3};
    }
    
    // this is a function that takes a pointer to an int, which the function will
    // then interpret as the beginning of an array of `length` ints
    void pointer_style(int* first, int length) {
        // an array of 32 uninitialized uint8_t
        std::uint8_t bytes[32];
    
        // can you spot the bug?
        for (int i = 0; i < length; ++i) {
            // this looks like "array syntax," but really it's pointer arithmetic
            bytes[i] = first[i];
            // also, an int can store more distinct values than a uint8_t can, so
            // this is a "narrowing conversion"
        }
    }
    
    // this is a function that takes a reference to an array of 32 ints
    void reference_style(int (&array)[32]) {
        // an array of 32 uninitialized uint8_t
        std::uint8_t bytes[32];
    
        for (int i = 0; i < 32; ++i) {
            bytes[i] = array[i];
        }
    }
    
    // this is a function that takes a vector of ints
    void standard_style(std::vector<int> numbers) {
        // bytes is initialized using the values in numbers
        std::vector<std::uint8_t> bytes(numbers.begin(), numbers.end());
    
        // now, bytes.size() == numbers.size()
    }
    
    int main() {
        // nothing to see here
    }
    Last edited by CodeMonkey; 08-01-2020 at 06:02 PM. Reason: one more example
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting clock_t to unsigned long
    By johnmerlino in forum C Programming
    Replies: 3
    Last Post: 04-19-2014, 11:28 AM
  2. Casting 0xFF (char) to 255 (unsigned int)
    By cyberfish in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2009, 11:21 AM
  3. Casting pointer to unsigned short
    By cks2k2 in forum C Programming
    Replies: 6
    Last Post: 03-25-2009, 05:33 AM
  4. Struct to Uint8_t array help please?
    By DavidDobson in forum C Programming
    Replies: 9
    Last Post: 01-20-2009, 08:17 AM
  5. int casting for unsigned chars
    By DarkMasterBosel in forum C++ Programming
    Replies: 4
    Last Post: 01-09-2008, 03:31 AM

Tags for this Thread