How to memcpy specifying starting and ending points...

Discussion in 'C++' started by Jona, May 9, 2007.

  1. Jona

    Jona Guest

    I'm really struggling with being able to do a memcpy but in a special
    way... I'm sure maybe there is some other mem... function to achive
    this...

    I need to copy everything except the few first bytes....

    ex:
    temp = abd9afd759a76e5d78a6; <- Copy this value only 4 bytes
    after ....

    //Using some function.... we process temp

    print (temp); // afd759a76e5d78a6; <-And I would get this...

    Using memcpy(temp2,temp,5); //would only copy "first" 5 bytes...

    Hope you understand thanks!

    -Jona
    Jona, May 9, 2007
    #1
    1. Advertising

  2. Jona

    Ian Collins Guest

    Jona wrote:
    > I'm really struggling with being able to do a memcpy but in a special
    > way... I'm sure maybe there is some other mem... function to achive
    > this...
    >
    > I need to copy everything except the few first bytes....
    >
    > ex:
    > temp = abd9afd759a76e5d78a6; <- Copy this value only 4 bytes
    > after ....
    >

    What type is temp?

    --
    Ian Collins.
    Ian Collins, May 9, 2007
    #2
    1. Advertising

  3. Jona

    Salt_Peter Guest

    On May 9, 1:06 am, Jona <> wrote:
    > I'm really struggling with being able to do a memcpy but in a special
    > way... I'm sure maybe there is some other mem... function to achive
    > this...
    >
    > I need to copy everything except the few first bytes....
    >
    > ex:
    > temp = abd9afd759a76e5d78a6; <- Copy this value only 4 bytes
    > after ....
    >
    > //Using some function.... we process temp
    >
    > print (temp); // afd759a76e5d78a6; <-And I would get this...
    >
    > Using memcpy(temp2,temp,5); //would only copy "first" 5 bytes...
    >
    > Hope you understand thanks!
    >
    > -Jona


    Look at std::string's assign(...) and substr(...) member functions.
    By the way, temp above is nothing at all, it has no type.
    Also "abcde" is a literal string but abcde is not.

    A much more effective way to ask your question would be something
    like:
    ___
    How can i write a function that will return a copy of the literal
    string shown below that excludes the first 4 characters?

    int main()
    {
    char* temp = "abd9afd759a76e5d78a6";
    }

    [hint - the above compiles]
    Salt_Peter, May 9, 2007
    #3
  4. Jona

    pmouse Guest

    On May 9, 1:50 am, Salt_Peter <> wrote:
    > On May 9, 1:06 am, Jona <> wrote:
    >
    >
    >
    > > I'm really struggling with being able to do a memcpy but in a special
    > > way... I'm sure maybe there is some other mem... function to achive
    > > this...

    >
    > > I need to copy everything except the few first bytes....

    >
    > > ex:
    > > temp = abd9afd759a76e5d78a6; <- Copy this value only 4 bytes
    > > after ....

    >
    > > //Using some function.... we process temp

    >
    > > print (temp); // afd759a76e5d78a6; <-And I would get this...

    >
    > > Using memcpy(temp2,temp,5); //would only copy "first" 5 bytes...

    >
    > > Hope you understand thanks!

    >
    > > -Jona

    >
    > Look at std::string's assign(...) and substr(...) member functions.
    > By the way, temp above is nothing at all, it has no type.
    > Also "abcde" is a literal string but abcde is not.
    >
    > A much more effective way to ask your question would be something
    > like:
    > ___
    > How can i write a function that will return a copy of the literal
    > string shown below that excludes the first 4 characters?
    >
    > int main()
    > {
    > char* temp = "abd9afd759a76e5d78a6";
    >
    > }
    >
    > [hint - the above compiles]


    he didn't say "abcde", he said abcde, which probably means 0xabcde
    and it seems like "temp" is a sequence of memory.
    So in that case, you would get the address of the beginning of temp:
    unsigned char * pBegin = &temp;
    and you move this pointer 4 "bytes" forward:
    pBegin+=4;
    and you copy:
    memcpy( &temp2, pBegin, a size here );

    Should be pretty basic...

    Regards,

    PQ
    pmouse, May 9, 2007
    #4
  5. Jona

    James Kanze Guest

    On May 9, 7:06 am, Jona <> wrote:
    > I'm really struggling with being able to do a memcpy but in a special
    > way... I'm sure maybe there is some other mem... function to achive
    > this...


    > I need to copy everything except the few first bytes....


    > ex:
    > temp = abd9afd759a76e5d78a6; <- Copy this value only 4 bytes
    > after ....


    > //Using some function.... we process temp


    > print (temp); // afd759a76e5d78a6; <-And I would get this...


    > Using memcpy(temp2,temp,5); //would only copy "first" 5 bytes...


    > Hope you understand thanks!


    Not really. What is temp?

    You can pass any valid address to memcpy, so copying just part
    of an array should be no problem. On the other hand, memcpy
    only works for very few types; in C++, I'd generally prefer
    std::copy (which will work for any type which supports copy).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 9, 2007
    #5
  6. Jona

    Jim Langston Guest

    "Jona" <> wrote in message
    news:...
    > I'm really struggling with being able to do a memcpy but in a special
    > way... I'm sure maybe there is some other mem... function to achive
    > this...
    >
    > I need to copy everything except the few first bytes....
    >
    > ex:
    > temp = abd9afd759a76e5d78a6; <- Copy this value only 4 bytes
    > after ....
    >
    > //Using some function.... we process temp
    >
    > print (temp); // afd759a76e5d78a6; <-And I would get this...
    >
    > Using memcpy(temp2,temp,5); //would only copy "first" 5 bytes...


    memcpy( temp2 + 4, temp, strlen( temp2 ) - 4 );

    >
    > Hope you understand thanks!
    >
    > -Jona
    >
    Jim Langston, May 9, 2007
    #6
  7. Jona

    Jona Guest

    Hey guys thanks a lot for you help, I should really made it more
    specific. It was a chunk of memory... using a UCHAR type...
    the memcpy(temp2+4,temp1,sizeof(temp1) - 4)
    did it! ;-)
    I apressiate it!
    -Jona
    Jona, May 9, 2007
    #7
  8. Jona

    Salt_Peter Guest

    On May 9, 2:31 am, pmouse <> wrote:
    > On May 9, 1:50 am, Salt_Peter <> wrote:
    >
    >
    >
    > > On May 9, 1:06 am, Jona <> wrote:

    >
    > > > I'm really struggling with being able to do a memcpy but in a special
    > > > way... I'm sure maybe there is some other mem... function to achive
    > > > this...

    >
    > > > I need to copy everything except the few first bytes....

    >
    > > > ex:
    > > > temp = abd9afd759a76e5d78a6; <- Copy this value only 4 bytes
    > > > after ....

    >
    > > > //Using some function.... we process temp

    >
    > > > print (temp); // afd759a76e5d78a6; <-And I would get this...

    >
    > > > Using memcpy(temp2,temp,5); //would only copy "first" 5 bytes...

    >
    > > > Hope you understand thanks!

    >
    > > > -Jona

    >
    > > Look at std::string's assign(...) and substr(...) member functions.
    > > By the way, temp above is nothing at all, it has no type.
    > > Also "abcde" is a literal string but abcde is not.

    >
    > > A much more effective way to ask your question would be something
    > > like:
    > > ___
    > > How can i write a function that will return a copy of the literal
    > > string shown below that excludes the first 4 characters?

    >
    > > int main()
    > > {
    > > char* temp = "abd9afd759a76e5d78a6";

    >
    > > }

    >
    > > [hint - the above compiles]

    >
    > he didn't say "abcde", he said abcde, which probably means 0xabcde
    > and it seems like "temp" is a sequence of memory.
    > So in that case, you would get the address of the beginning of temp:
    > unsigned char * pBegin = &temp;
    > and you move this pointer 4 "bytes" forward:
    > pBegin+=4;
    > and you copy:
    > memcpy( &temp2, pBegin, a size here );
    >
    > Should be pretty basic...
    >
    > Regards,
    >
    > PQ


    basic enough to prompt the question:
    In your opinion, whats the numeric range of temp's type?
    Salt_Peter, May 9, 2007
    #8
  9. Jona

    Peter Qian Guest

    "Salt_Peter" <> wrote in message
    news:...
    > On May 9, 2:31 am, pmouse <> wrote:
    >> On May 9, 1:50 am, Salt_Peter <> wrote:
    >>
    >>
    >>
    >> > On May 9, 1:06 am, Jona <> wrote:

    >>
    >> > > I'm really struggling with being able to do a memcpy but in a special
    >> > > way... I'm sure maybe there is some other mem... function to achive
    >> > > this...

    >>
    >> > > I need to copy everything except the few first bytes....

    >>
    >> > > ex:
    >> > > temp = abd9afd759a76e5d78a6; <- Copy this value only 4 bytes
    >> > > after ....

    >>
    >> > > //Using some function.... we process temp

    >>
    >> > > print (temp); // afd759a76e5d78a6; <-And I would get
    >> > > this...

    >>
    >> > > Using memcpy(temp2,temp,5); //would only copy "first" 5 bytes...

    >>
    >> > > Hope you understand thanks!

    >>
    >> > > -Jona

    >>
    >> > Look at std::string's assign(...) and substr(...) member functions.
    >> > By the way, temp above is nothing at all, it has no type.
    >> > Also "abcde" is a literal string but abcde is not.

    >>
    >> > A much more effective way to ask your question would be something
    >> > like:
    >> > ___
    >> > How can i write a function that will return a copy of the literal
    >> > string shown below that excludes the first 4 characters?

    >>
    >> > int main()
    >> > {
    >> > char* temp = "abd9afd759a76e5d78a6";

    >>
    >> > }

    >>
    >> > [hint - the above compiles]

    >>
    >> he didn't say "abcde", he said abcde, which probably means 0xabcde
    >> and it seems like "temp" is a sequence of memory.
    >> So in that case, you would get the address of the beginning of temp:
    >> unsigned char * pBegin = &temp;
    >> and you move this pointer 4 "bytes" forward:
    >> pBegin+=4;
    >> and you copy:
    >> memcpy( &temp2, pBegin, a size here );
    >>
    >> Should be pretty basic...
    >>
    >> Regards,
    >>
    >> PQ

    >
    > basic enough to prompt the question:
    > In your opinion, whats the numeric range of temp's type?
    >


    To me he wasn't using "temp" as a varible, just a notation to indicate a
    sequence of memory.

    Really poor phrased question anyway.

    Regards,

    PQ
    Peter Qian, May 9, 2007
    #9
  10. Jona

    Jim Langston Guest

    "Jona" <> wrote in message
    news:...
    > Hey guys thanks a lot for you help, I should really made it more
    > specific. It was a chunk of memory... using a UCHAR type...
    > the memcpy(temp2+4,temp1,sizeof(temp1) - 4)
    > did it! ;-)
    > I apressiate it!


    Incidently, this being a c-style string, if you simply need to print, or
    pass to a function, ignoring the first 4 characters you don't really have to
    copy into a temp first if you don't want. Using your pseudo code example
    of:

    print(temp);
    you could say
    print(temp+4);

    Just make darn sure that the length of the c-string temp is greater than 4
    :D
    Jim Langston, May 10, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Srinivasa T.N.
    Replies:
    7
    Views:
    537
    Amy G
    Dec 1, 2003
  2. Mayeul
    Replies:
    0
    Views:
    965
    Mayeul
    Jul 11, 2011
  3. Joe Kesselman
    Replies:
    2
    Views:
    884
    Joe Kesselman
    Jul 12, 2011
  4. Mayeul
    Replies:
    0
    Views:
    960
    Mayeul
    Jul 12, 2011
  5. Joe Kesselman
    Replies:
    0
    Views:
    751
    Joe Kesselman
    Jul 12, 2011
Loading...

Share This Page