Copy Unsigned Char Array Into Another

Hello all,

I am having some trouble performing this. I am not sure, if my unsigned char arrays are null terminated, but I don't think so. Here is my code:
They are supposed to be byte arrays of size 16.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int setkey(unsigned char* ky)
	{
		
		printf("INSIDE POLY-DEL ... key byte array  passed in HEX: \n");
		int i;
		for (i = 0; i < (int)16; i++)
		{
		    if (i > 0) printf(":");
		    printf("%02X", ky[i]);
		}
		printf("\n");
		printf("sizeofky%lu, sizeofHMAC %lu \n\n",sizeof(ky), sizeof(HMAC_ky) );
		memcpy(&this->HMAC_ky, &ky,16 ); 	//memcpy(target, source, sourcelen+1)

		printf("sizeofky %lu.INSIDE POLY-DEL ...HMAC key byte array in HEX: \n", sizeof(ky));
		for (i = 0; i < (int)16; i++)
		{
		    if (i > 0) printf(":");
		    printf("%02X", HMAC_ky[i]);
		}
		printf("\n");

	//	this->HMAC_ky=ky;
		return 0;
	}


Any help?
Last edited on
http://www.cplusplus.com/reference/cstring/strcpy/

May be of any help.

I would suggest using strings instead, unless you insist on C-strings.
Thank you, but I am using unsigned char arrays. I don't think strcpy works for unsigned char, does it?
I could cast it, but I would lose precision. My values are all positive.
Last edited on
Play around with it. Normally I'm able to cast from signed int to unsigned int (-1 cast into 4294967295) and back into -1 when I cast into signed int:

1
2
std::cout << static_cast<unsigned int>(-1) << std::endl;
std::cout << static_cast<signed int>(static_cast<unsigned int>(-1)) << std::endl;
So, there is really no way to copy directly one unsigned char array to another easily?
They are the same data type.
I guess I will enclose them in a loop and pass each char one by one.

Also, why is that memcpy is not working in my first post?
Last edited on
without knowing the details of this, I do think that memcpy should work! I don't see why not, I have used it in the past and can't remember any issues I had with it
1
2
3
4
5
int setkey(unsigned char* ky)
{
    // ...
    memcpy(&this->HMAC_ky, &ky,16 );
    // ... 



Also, why is that memcpy is not working in my first post?


&ky is the address of a pointer. You want to copy from the address held by ky, not from the address of ky which certainly doesn't hold 16 bytes of data.

Depending on the type of HMAC_ky (you don't need to use this->HMAC_ky,) the call should probably look more like this:

memcpy(HMAC_ky, ky, 16);

In C++ you should probably prefer to use std::copy, which might look like the following:

1
2
3
4
#include <algorithm>
    // ...
    std::copy( ky, ky+16, HMAC_ky ) ; 
    // ... 

Last edited on
+1 for std::copy
@Cubbi
How will you pass an iterator of unsigned char * to std::copy?
unsigned char * is a perfectly acceptable iterator.
How will you pass an iterator of unsigned char * to std::copy?


As shown. Pointers satisfy the criteria for iterators.
Ah, I get it now. +1 for std::copy then!
Thank you very much, Cire :)
Topic archived. No new replies allowed.