简体   繁体   中英

Unsigned char array concatenation in C

What's the best way to concatenate unsigned char arrays in C? Furthermore, is there a way to concatenate unsigned char arrays with char arrays? 2 of these unsigned char arrays are really just strings, but for simplicity, I'm treating them as unsigned char arrays.

The requirement is complex: there is a function that will take 1 (one) unsigned char array. That one unsigned char array is really 4 variables concatenated to make up that 1 unsigned char array. To add to the complexity, the first unsigned char array is really just a string of variable length, but its max length is 60 (ie sometimes it would have length = 15, other times = 60).

someFunctionAssignsFirst(unsigned char *first) 
{
   //it could be 15 or 60 chars long.
   ...
}

unsigned char first[60] = //someFunctionAssignsFirst() //This is a string i.e. "variable size string max size 60"
unsigned char second[8] = "always8."; //This is a string i.e. "01234567"
unsigned char third[32] = "always32"; //This is a cryptographic key
unsigned char fourth[32] = "always32"; //This is a cryptographic key

How would I go about getting:

unsigned char allstrings[sizeof(first)+sizeof(second)+sizeof(third)+sizeof(fourth)] = //all strings combined

?

I attempted some for loops, but the variable length first is disrupting the concatenation, and I'm sure there has to be a better way.

Full Disclosure: I'm not an expert, and I don't necessarily love C. Also for the requirement, not allowed C++ or any other language.


This is what I was trying to do, and (for clarification) I don't get a null character at the end so it's not really a string.

unsigned char *first = "this is a sample string, human readable";
unsigned char *second = "12345678" //always a number
//unsigned char third -> I have the value from before and it's a key
//unsigned char fourth -> I have the value from before and it's a key
unsigned char allstrings[sizeof(first) + sizeof(second) + sizeof(third) + sizeof(fourth)];
    int counter = 0;
    for (int i = 0; i <= sizeof(first); i++)    
    {
        allstrings[counter] = first[i];
        counter++;
    }
    for (int i = 0; i <= sizeof(second); i++)       
    {
        allstrings[counter] = second[i];
        counter++;
    }
    for (int i = 0; i <= sizeof(third); i++)
    {
        allstrings[counter] = third[i];
        counter++;
    }
    for (int i = 0; i <= sizeof(fourth); i++)
    {
        allstrings[counter] = fourth[i];
        counter++;
    }

The allstrings variable, doesn't get anything beyond "readable" in my example above.

You need to use strcpy to copy over the first part, which is a string, then use memcpy to copy over the other 3, which are not strings but char arrays.

Note that the result is not a string but a char array, ie it is not null terminated.

unsigned char allstrings[strlen(first)+sizeof(second)+sizeof(third)+sizeof(fourth)];
strcpy(allstrings,first);
memcpy(allstrings+strlen(first),second,sizeof(second));
memcpy(allstrings+strlen(first)+sizeof(second),third,sizeof(third));
memcpy(allstrings+strlen(first)+sizeof(second)+sizeof(third),fourth,sizeof(fourth));

I guess you want to treat the array as buffer . So it's fine to have the declarations, but you don't need to define the content for this moment:

unsigned char first[60];
unsigned char second[8];
unsigned char third[32];
unsigned char fourth[32];
#define ALLSTRLEN sizeof(first) + sizeof(second) + sizeof(third) + sizeof(fourth)
unsigned char allstrings[ALLSTRLEN];

The code will keep the fixed size of arrays. and please notice that the arrays should be global or static for safety reasons.

Then you can copy the contents to arrays. I just put your code under main() to concatenate these arrays:

int main()
{
    strcpy((char *)first, "this is a sample string, human readable");
    // do something for second, third, fourth....
    //

    int counter = 0;
    // first array is a normal string, we have to copy null character for it
    for (int i = 0; i <= strlen((char *)first)+1; i++)    
    {
        allstrings[counter] = first[i];
        counter++;
    }
    for (int i = 0; i <= sizeof(second); i++)       
    {
        allstrings[counter] = second[i];
        counter++;
    }
    for (int i = 0; i <= sizeof(third); i++)
    {
        allstrings[counter] = third[i];
        counter++;
    }
    for (int i = 0; i <= sizeof(fourth); i++)
    {
        allstrings[counter] = fourth[i];
        counter++;
    }
    // allstrings is finished
}

Please notice this example just works in main() function; if you call a function to concatenate four arrays, the compiler has to pass the arrays as pointers, and the sizeof() will be wrong (equal to the pointer's size). You can test the size by doing this:

printf("sizeof(second)=%d\n", sizeof(second));

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