Originally Posted by
orientuser
The binary code of one of the 2 bytes in y is 10010110. If this byte is regarded as a signed char, its value is just -106.
BTW: A plain char might be regarded as signed char or unsigned char, depending on the compiler you are using.
Many thanks to you for the pointer !
I modified the program as follows:
Code:
#include <stdio.h>
#include <string.h> // memcpy
#include <iostream>
using namespace std;
int main ()
{
unsigned char *x = new unsigned char [20];
unsigned short *y = new unsigned short [2];
*y = 255;
memcpy (x, (const void *)&y, 2);
printf ("\n%d\n", x[0]);
printf ("\n%d\n", x[1]);
}
Now the output is 48 and 32 !!
Shouldn't the output be 255 and 0 ?