Thread: Moving data

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    69

    Moving data

    Hey is there fast way to move data between different types?

    I had in delphi function called move so i could do like this :

    Code:
    a,b : Integer;
    buff : array[0..3] of byte
    ....
    move(a, buff[0], 4);
    move(buff[0], b, 4);
    Is something like that in C++? I need to store integers and long ints into buffer which i need to send via TCP.

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    In C++, you just swap variable values.... Do you know the concept of swapping?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Hm, not really

    Do you mean something like (type *) (void) (int) ?

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    No , like,
    Code:
    int x=1;
    int y=2;
    int z=x;
    x=y;
    y=z;
    This is called swapping...

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Quote Originally Posted by Mr.777 View Post
    No , like,
    Code:
    int x=1;
    int y=2;
    int z=x;
    x=y;
    y=z;
    This is called swapping...
    Ohh i know what you are talking about but, what swapping have to do with this

    Code:
    unsigned long a;
    BYTE myBuff[0xFFFF];
    ..
    //How to place (a) into buffer starting from 20th pos, myBuff[20]
    //So my number will be as hex in myBuff pos 20 to 24.

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I think, pointers can help you......
    As pointers directly refers to the memory addresses.
    May be something like this....
    Code:
    BYTE * ptr=myBuff[0];
    ptr+=20;
    *ptr=a;

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You could learn about bit manipulation and make an attempt that way. In particular shifting and AND are going to be useful. OTOH, memcpy will probably work too.

  8. #8
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Instead of using memcpy, i'll recommend memmove........

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Which one is correct? That later i everybody can read from buffer corectly not from behind?

    Code:
    buff[3] = a;
    buff[2] = a >> 8;
    buff[1] = a >> 16;
    buff[0] = a >> 24;
    Code:
    buff[3] = a >> 24;
    buff[2] = a >> 16;
    buff[1] = a >> 8;
    buff[0] = a;

  10. #10
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I belive first one as you will shift to the number of bits after you get the value...
    And the second one will shift you more far......
    What do you say?

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    So i can shift like that whole string to buff increasing number of bits by 8 every time?

  12. #12
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Yes, you can.....

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    What's wrong with this def?

    Code:
    typedef BYTE[4] BLOCK;

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A typedef looks exactly the same as a variable declaration, except for the typedef keyword.

    Code:
    BYTE block[4]; //block is an array
    
    typedef BYTE block[4]; //block is another name for BYTE[4]
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parent in a binary search tree
    By roaan in forum C Programming
    Replies: 4
    Last Post: 08-26-2009, 07:08 PM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM