unsigned char* EncodeCommonStruct(const CommonStruct_st& CommonSt)
{
//分配内存
unsigned char* strBuff = (unsigned char*)malloc(CALC_COMMON_ST_LEN(&CommonSt));
if (NULL == strBuff)
{
return NULL;
}
//填充内容
*(long*)strBuff = CommonSt.len;
if (CommonSt.len > 0) {
memcpy(strBuff + sizeof(long), CommonSt.buff, CommonSt.len);
}
return strBuff;
}
BOOL DecodeCommonStruct(const unsigned char* strBuff, long len, CommonStruct_st& CommonSt)
{
long st_len;
if (NULL == strBuff)
{
return FALSE;
}
//获取到当前长度
st_len = *(const long*)strBuff;
//校验BUFF内容合法性
if (st_len + sizeof(long) > len) {
return FALSE;
}
CommonSt.len = st_len;
CommonSt.buff = (void*)malloc(st_len);
memcpy(CommonSt.buff, strBuff + sizeof(long), st_len);
return TRUE;
}
typedef struct tagCommonStruct {
long len;
void* buff;
}CommonStruct_st;
C++结构体序列化和反序列化方法
最新推荐文章于 2024-08-30 16:38:07 发布
2571

被折叠的 条评论
为什么被折叠?



