?
Solved

Join more than 1 lpByte to one

Posted on 2011-10-20
3
Medium Priority
?
487 Views
Last Modified: 2013-12-14
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?
0
Comment
Question by:Wuotan
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 2000 total points
ID: 36999087
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,

ZOPPO
0
 

Author Closing Comment

by:Wuotan
ID: 36999411
You are truly a genius!
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 36999418
Thanks :o)

I'm glad I could help ...

Have a nice day,

best regards,

ZOPPO
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question