简体   繁体   中英

Passing uint8_t array argument to the subroutine that excepts uint32_t (conversion from uint8_t array to uint32_t array)

I'm having a C subroutine as below,

   int sub_data (uint32_t size, uint32_t arrayB[])
   {
     int i;
     for(i=0; i<size; i++)
       printf("arrayB[%0d] = %2x\n", i , arrayB[i]);
   }

And I want pass arrayA as below to above mentioned subroutine,

   uint8_t arrayA = {0x12, 0x34, 0x56, 0x78, 0x9a};

   int main()
   {
     sub_data(5, arrayA);
     return 0;
   }

The issue is arrayA is of type uint8_t and sub_data excepts argument as uint32_t My requirement is sub_data should take single word from arrayA and store inside arrayB as single word

So final print should be,

arrayB[0] = 12
arrayB[1] = 34
arrayB[2] = 56
arrayB[3] = 78
arrayB[4] = 9a.

I'm struggling to do the conversion. And all the solution I came around is to store four words of arrayA to single word of arrayB. But I want single word of arrayA as single word of arrayB.

How do I achieve that?

You could use memcpy to fill a uint32_t array with each uint8_t array element, and pass it to the function:

uint8_t arrayA[5] = {0x12, 0x34, 0x56, 0x78, 0x9a};

uint32_t arrayB[5] = {0, 0, 0, 0, 0};

for (int i=0; i<5; i++)
    memcpy(&arrayB[i], &arrayA[i], sizeof(arrayA[i]));

sub_data(5, arrayB);

You can use generic macros and have more than one function to handle the parameter.

Example:

int sub_data_32 (uint32_t size, uint32_t *arrayB)
{
    int i;
    for(i=0; i<size; i++)
        printf("arrayB[%0d] = %2x\n", i , arrayB[i]);
}

int sub_data_16 (uint32_t size, uint16_t *arrayB)
{
    int i;
    for(i=0; i<size; i++)
        printf("arrayB[%0d] = %2x\n", i , arrayB[i]);
}

int sub_data_8 (uint32_t size, uint8_t *arrayB)
{
    int i;
    for(i=0; i<size; i++)
        printf("arrayB[%0d] = %2x\n", i , arrayB[i]);
}

#define sub_data(a,b) _Generic((b), \
              uint8_t *:  sub_data_8, \
              uint16_t *: sub_data_16, \
              uint32_t *: sub_data_32) (a,b)

uint8_t arrayA[] = {0x12, 0x34, 0x56, 0x78, 0x9a};
uint16_t arrayB[] = {0x13, 0x35, 0x57, 0x79, 0x9b};
uint32_t arrayC[] = {0x14, 0x36, 0x58, 0x7A, 0x9c};

int main()
{
    sub_data(5, arrayA);
    printf("-----------\n");
    sub_data(5, arrayB);
    printf("-----------\n");
    sub_data(5, arrayC);
    return 0;
}

https://onlinegdb.com/BJTqCPyJL

int sub_data (uint32_t size, uint32_t arrayB[])
   {
     int i;
     for(i=0; i<size; i++)
       printf("arrayB[%0d] = %2x\n", i , arrayB[i]);
   }

   uint8_t arrayA = {0x12, 0x34, 0x56, 0x78, 0x9a};

   int main()
   {
     uint32_t temp[5] = {0};

     int i;  
     for(i=0; i<5; i++)
       temp[i]=arrayA[i];

     sub_data(5, temp);
     return 0;
   }

Here I'm assigning data one by one to uint32_t temp from uint8_t arrayA like this,

uint32_t temp[5] = {0};

     int i;  
     for(i=0; i<5; i++)
       temp[i]=arrayA[i];

This works perfectly fine. Thank you @rici for the comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM