This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Can memcpy() use in this way?

When needed read one unsigned long type variable from uart_buffer, the code following is false,how correct it?

like:
...
#include<string.h>
uchar x[2408]={0x12,0x34,0x56,0x78,...};
unsigned long lTemp=0;
unsigned int iDay = 2;
...
memcpy(&lTemp,&x[0],4);
//result: lTemp =0x12345678 NOT 0X78563412;
lTemp =0x12345678/1000 - iDay * 3600 * 24;
//result: lTemp = 0x3b48b NOT 0XB48B
...

Parents
  • You have an endianess mismatch somewhere in your system. The 8051, since it's an 8-bit processor, has no endianness to worry about. Keil C stores multi-byte integers in big endian format, most significant byte first.

    You'll need to write a function (or macro) that byte-swaps the data in your input buffer to match the local representation.

    You might be able to do a swap in place, if you don't mind altering your input value.

Reply
  • You have an endianess mismatch somewhere in your system. The 8051, since it's an 8-bit processor, has no endianness to worry about. Keil C stores multi-byte integers in big endian format, most significant byte first.

    You'll need to write a function (or macro) that byte-swaps the data in your input buffer to match the local representation.

    You might be able to do a swap in place, if you don't mind altering your input value.

Children
No data