Hello,

I have the following code that works fine for packing and unpacking with memcpy:

#include <string>
using namespace std;

Code:
int main()
{
    // PACK
    char *bufptr; 
    char buf[12];
    unsigned int data1 = 1003;
    unsigned int data2 = 0;
    float data3 = 90.0;
    bufptr = buf; 
    memcpy(bufptr, &data1, 4);
    bufptr+=4;
    memcpy(bufptr, &data2, 4);
    bufptr+=4;
    memcpy(bufptr, &data3, 4);
    bufptr+=4;

    // UNPACK
    unsigned int _data1, _data2;
    float _data3;
    char *temp = buf;
    memcpy(&_data1, temp, 4); 
    temp+=4;
    memcpy(&_data2, temp, 4); 
    temp+=4;    
    memcpy(&_data3, temp, 4); 
    temp+=4; 
    return 0;
}

but now I tried to add strings into the mix, and it stops working. What am I doing wrong with the strings here?

#include <string>
using namespace std;

Code:
int main()
{
    // PACK
    char *bufptr; 
    char buf[12];
    unsigned int data1 = 1003;
    unsigned int data2 = 0;
    float data3 = 90.0;
    bufptr = buf; 
    memcpy(bufptr, &data1, 4);
    bufptr+=4;
    memcpy(bufptr, &data2, 4);
    bufptr+=4;
    memcpy(bufptr, &data3, 4);
    bufptr+=4;
    string message=buf;

    // UNPACK
    unsigned int _data1, _data2;
    float _data3;
    char *temp = (char*)message.c_str();
    memcpy(&_data1, temp, 4); 
    temp+=4;
    memcpy(&_data2, temp, 4); 
    temp+=4;    
    memcpy(&_data3, temp, 4); 
    temp+=4; 
    return 0;
}

I am guessing trhe line that is wrong is this line:

char *temp = (char*)message.c_str();

Can anyone help me to make this work correctly with strings?

Thanks!!