Others have not explained why your code does not work, so I will quickly hit it:
memcpy(pshort , pchar + 1 , 1); memcpy(pshort + 1, pchar , 1);
Adding TYPE * p to the pointer moves the pointer using sizeof( TYPE ) increments (so it points to the next element, remember that this is defined only inside the array). Therefore, if pchar + 1 correct, pshort + 1 not (since it refers to the next short ).
aux = ((*pchar & 0x00FF) << 8) | ((*(pchar+1) & 0xFF00) >> 8);
Errr .... the right side is broken more than one. Firstly, *(pchar+1) is a char , and & 0xFF00 on a char will always give 0 (because a char is only 8 bits, at least on modern machines ...). And then you shift these 8 bits to the right ...?
And just in case, when you did not know about it, if you did not use 0x00FF on the left side (advancing *pchar in the width of the right operand), but ( char size) 0xFF, the result of this operation will still be of type char , and shifting this 8 bits to the left doesn't make much sense either (since the type doesn't expand magically).
Another way to do this hasn't mentioned union yet:
If you use this in a wider context, beware of debugging the structure.
source share