Join more than 1 lpByte to one

Posted on 2011-10-20
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?
Question by:Wuotan
    LVL 30

    Accepted 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,


    Author Closing Comment

    You are truly a genius!
    LVL 30

    Expert Comment

    Thanks :o)

    I'm glad I could help ...

    Have a nice day,

    best regards,


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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…
    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now