I want these two print functions to do the same thing:
unsigned int Arraye[] = {0xffff,0xefef,65,66,67,68,69,0};
char Arrage[] = {0xffff,0xefef,65,66,67,68,69,0};
printf("%s", (char*)(2+ Arraye));
printf("%s", (char*)(2+ Arrage));
where Array is an unsigned int. Normally, I would change the type but, the problem is that most of the array is numbers, although the particular section should be printed as ASCII. Currently, the unsigned array prints as “A” and the char array prints as the desired “ABCDE”.
This is how the
unsigned intversion will be arranged in memory, assuming 32-bit big endian integers.This is how the
charversion will be arranged in memory, assuming 8-bit characters. Note that0xffffdoes not fit in achar.So you can see, casting is not enough. You’ll need to actually convert the data.
If you know that your system uses 32-bit
wchar_t, you can use thellength modifier forprintf.This is NOT portable. The alternative is to copy the
unsigned intarray into achararray by hand, something like this: