简体   繁体   中英

Does memcpy copy bytes in reverse order?

I am little bit confused on usage of memcpy. I though memcpy can be used to copy chunks of binary data to address we desire. I was trying to implement a small logic to directyl convert 2 bytes of hex to 16 bit signed integer without using union.

 #include <stdio.h>
    #include <stdint.h>
    #include <string.h>
    int main()
    {   uint8_t message[2] = {0xfd,0x58};
        // int16_t roll = message[0]<<8;
        // roll|=message[1];
        int16_t roll = 0;
        memcpy((void *)&roll,(void *)&message,2);
        printf("%x",roll);
    
        return 0;
    }

Does memcpy copy bytes in reverse order?

<\/blockquote>

No, memcpy<\/code> did not reverse the bytes as it copied them. That would be a strange and wrong thing for memcpy<\/code> to do.

There's probably a canonical answer on this somewhere, but here's what you need to understand about byte order, or " endianness<\/a> ".

Suppose I write this little code fragment:

#include <stdio.h>

char string[] = "Hello";
printf("address of string:   %p\n", (void *)&string);
printf("address of 1st char: %p\n", (void *)&string[0]);
printf("address of 5th char: %p\n", (void *)&string[4]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM