Thread: can copy directly???

  1. #1
    Registered User
    Join Date
    Sep 2006

    can copy directly???

    sorry if my question sound stupid..^_^

    but can i copy directly using memcpy()

    unsigned char check_sum[4];
    unsigned long sum;
    if the stored data are in hex format and the value of sum = 63ADCE11, can i copy it into check_sum? both using 4 byte storage right?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Out of scope
    I'm not sure longs are necessarily all 4 bytes... however, I managed to make this work:
    #include <stdio.h>
    #include <string.h>
    int main(void) {
       unsigned __int32 foo;
       unsigned char bar[4] = { 0xFF, 0x00, 0x00, 0x00 }; 
       return 0;
    The output for me was 255... the first thing you should notice about that is the bytes are(or may be) in the reverse order than you might expect. I'd suppose this has to do with the endian of my computer. You can probably fix this by making sure you convert the byte order with one of the socket functions like htonl(). Other than that, it seems to work fine. I wouldn't be surprised if there were some other quirks I don't notice here... I also wouldn't be surprised if the output was different on your computer. Someone will be able to enlighten you further.
    Last edited by SlyMaelstrom; 09-25-2006 at 09:43 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Sep 2006
    ok thnx..i'll try it..

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    Here's another neat not-quite-standard trick:
    union foo
      unsigned char check_sum[4];
      unsigned long sum;
    And then you can set check_sum to whatever you need and retrieve it as a long using sum.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    While we're using non-standard tricks . . . I think this would work:
    *(unsigned long *)check_sum
    I don't know how you got an array like this in the first place.
    unsigned char check_sum[4];
    If you read it in from a file, you could use fread().

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to copy a C struct to a C++ class?
    By Ptbamboo in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 02:11 PM
  2. Copying constant amount of data
    By TriKri in forum C++ Programming
    Replies: 16
    Last Post: 07-12-2008, 06:32 AM
  3. Copy constructor question
    By BigFish21 in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2008, 12:18 PM
  4. Copy Function changing value?
    By GMHummerH1 in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2004, 10:04 AM
  5. Quick ? on copy constructor
    By Traveller in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2002, 10:31 AM