采用移位的形式实现:
int realLength = dataLength + sizeof(int);//dataLength是数据的长度
char *sendData = new char[realLength];//sendData是最后得到的数据
memset(sendData, 0, realLength*sizeof(char));//sendData初始化
int num1, num2, num3, num4;
char *p = (char *)&dataLength;
num1 = ((int)*p);
num2 = ((int)*(p+1));
num3 = ((int)*(p+2));
num4 = ((int)*(p+3));
memcpy(sendData, &num4, sizeof(int));
memcpy(sendData+sizeof(char), &num3, sizeof(int));
memcpy(sendData+2*sizeof(char), &num2, sizeof(int));
memcpy(sendData+3*sizeof(char), &num1, sizeof(int));
memcpy(sendData+sizeof(int), out.c_str(), out.size());