How to memcpy specifying starting and ending points...

J

Jona

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
 
I

Ian Collins

Jona said:
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?
 
S

Salt_Peter

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]
 
P

pmouse

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!

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
 
J

James Kanze

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).
 
J

Jim Langston

Jona said:
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 );
 
J

Jona

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
 
S

Salt_Peter

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?
 
P

Peter Qian

Salt_Peter said:
On May 9, 1:06 am, Jona <[email protected]> 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!

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
 
J

Jim Langston

Jona said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,218
Latest member
NatishaFin

Latest Threads

Top