Thread: Does memcpy cause endian flip?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485

    Does memcpy cause endian flip?

    Am I doing something wrong here or what is it that cause the number to come out backwards?

    Code:
    	unsigned char bytes[3];	
    	unsigned short large = 0x87E5; 
    
    	printf("%x\n", large);
    	
    	memcpy(&bytes, &large, 2);
    	printf("%x%x\n", bytes[0], bytes[1]);
    This is the result:

    87e5
    e587

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Your PC is a little endian machine. That's simply the way it is stored. In other words, everything is working as designed.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Hmm, ok thats backwards as far as I as a human see thigs but ok. Suppose that I need to do bit manipulations on the first value would I need to use the htons function on it first for it to come out right?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    If you do bit operations on a short, as a short, the system will work as you expect.
    If you do bit operations on a char, as a char, the system will work as you expect.
    ... etc for all datatypes.

    If you do bit operations on a short as a char, or vice versa, you will have to deal with the hardware implementation (big or little endianness) of the platform you are programming on.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Dino View Post
    If you do bit operations on a short as a char..

    Do you mean if I break up a short into two chars as above?
    Last edited by Subsonics; 01-17-2009 at 04:18 PM.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yes.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Thanks,

    Is there a way to print a number as binary with printf BTW?

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Not with a built-in formatter. I bet you're smart enough to be able to write a loop to do it though.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  2. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  3. Big and little endian
    By Cactus_Hugger in forum C Programming
    Replies: 4
    Last Post: 10-12-2005, 07:07 PM
  4. Replies: 10
    Last Post: 06-26-2005, 11:27 AM
  5. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM