I would copy a uint8_t array in a char array. I tried different solutions with: cast, memcopy, strcpy... but it does not work!!! My little example is:
uint32_t num = 123456789;
printf("\n num: %"PRIu32 "\n", num);
uint32_t x;
uint8_t a[4];
char b[4];
num=htonl(num);
a[0]=num;
a[1]=num>>8;
a[2]=num>>16;
a[3]=num>>24;
b[0] = a[0];
b[1] = a[1];
b[2] = a[2];
b[3] = a[3];
printf("\nA: %x %x %x %x", a[0],a[1],a[2],a[3]);
printf("\nB: %x %x %x %x", b[0],b[1],b[2],b[3]);
x= b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24;
x=ntohl(x);
printf("\n x vale: %"PRIu32 "\n", x);
}
The prints are:
num: 123456789
A: 7 5b cd 15
B: 7 5b ffffffcd 15
x: 123457023
Why I get a differente number in x?