• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

Join more than 1 lpByte to one

I have a vector<LPBYTE> and want to join all the elements in this vector to only one LPBYTE.
This is what I tried:
LPBYTE lpByte;
for(int i = 0;i<vecLPB.size();++i)
	lpByte += vecLPB[i]; //i tried it like this, however this will just give me errors.

Open in new window

How can I manage to do this?
  • 2
1 Solution
Hi Wuotan,

a LPBYTE is a pointer to a byte. Usually pointers can be used in two different ways:
1. the pointer points to a single instance, i.e.:
BYTE byte;
LPBYTE lp = &byte;
2. the pointer points to the first instance of a block of data (like in C-style arrays), i.e.:
BYTE bytes[] = { 1, 2, 3, 4 };
LPBYTE lp = bytes;

You cannot simply add pointers in a senseful way since they just keep the address of the data, they don't represent the data itself. Instead you have to somehow concatenate the data pointed to by the LPBYTE stored in vecLPB. To do so you first will have to allocate a new buffer which has to be large enough to hold the data of all those data blocks.

The problem with the code fragment you gave is that it's not possible to tell you how to find the needed size for the data to allocate. If you use the pointer as in 1.) from above then it's simply 'sizeof( BYTE ) * vecLPB.size()'. But if your pointers are used as in 2.) from above you need to know (and build the sum of) the sizes of the data blocks each single pointer in vecLPB points to.

As soon as you have the size you need you can concatenate the data somehow like this:
LPBYTE lpDest = new BYTE[ nAllSize ]; // the 'nAllSize' is the size of all data
LPBYTE lpInsert = lpDest;
for ( int i = 0; i < vecLPB.size(); ++i )
 int nSize = ...; // here you need to set the size of the 'i'-th data block here
 memcpy( lpInsert, vecLPB[i], nSize );
 lpInsert += nSize;

Open in new window

Hope that helps,

WuotanAuthor Commented:
You are truly a genius!
Thanks :o)

I'm glad I could help ...

Have a nice day,

best regards,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now