Append data of Short, Int, Long Long... to char*
<p>I want to append the raw data of anything from <code>short</code>, <code>int</code>, <code>long long</code> to <code>byte</code> and <code>bool</code> to a <code>char*</code> or <code>void*</code>.</p>

<p>Here is what I came up with:</p>

<pre><code>template &lt;typename T&gt;
static void memcopyToBytestring(T&amp; target, size_t targetSize, char* previeous, size_t previousSize)
{
    if (target &amp;&amp; targetSize &gt; 0)
    {
        char newByteString[previousSize + targetSize];

        memcpy(newByteString, previeous, previousSize); //copy old
        memcpy(newByteString + previousSize, &amp;target, targetSize); //add new

        previousSize += targetSize;
        previeous = new char[previousSize];

        memcpy(previeous, newByteString, previousSize);
    }
}
</code></pre>

<p>Something seems to be off and I don't even know what and how to correctly test this (look at the data of <code>target</code>)</p>

<p>Use case: I need to serialize data according to a predefined protocol to send the bytes out.</p>

<p>Question: how can I correctly append data to a char array?</p>
