结构体转字符串

liyapeng90 2012-12-27 10:48:15
typedef struct _tag_M2M_HEAD
{
int size;
int cmdid;
int seqid;
int versionnum;
char SecurityID;
char ReserveWord;
int terminalID;

}M2M_HEAD;

//数据包
typedef struct _tag_M2M_PACKAGE
{
M2M_HEAD head; // 数据报的包头

//数据包包体
char data[100];
int n;
time_t t;

} M2M_PACKAGE;



如果M2M_PACKAGE这个结构体中有数据,怎么把那些数据转化成字符串,并且其他地方收到这个字符串还可以还原成现在的结构体啊,网上我看了好多方法,都没有用,不知道是不是结构体中嵌套了一个结构体这样不行啊,求各位指教啊,最好有个源码
...全文
586 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhcosin 2013-01-10
  • 打赏
  • 举报
回复
引用 21 楼 liyapeng90 的回复:
引用 17 楼 zhcosin 的回复:有两个小错误,更正了一下,通过了测试: C/C++ code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172……
随便写的,足够大以容纳内容即可。
liyapeng90 2012-12-28
  • 打赏
  • 举报
回复
引用 17 楼 zhcosin 的回复:
有两个小错误,更正了一下,通过了测试: C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182……
有个小小的问题,你那个size为什么要那么计算啊,128是干嘛的
东大坡居士 2012-12-27
  • 打赏
  • 举报
回复

typedef struct _tag_M2M_PACKAGE
{
M2M_HEAD head; // 数据报的包头

//数据包包体
char data[100];
int n;
time_t t;

} M2M_PACKAGE;

//转换成字符串的时候增加各个字符标志:
比如:
M2M_PACKAGE a = {NULL, {0}, 1, 0};
"M2M_PACKAGE:a:NULL#0#1#0"
读的时候还原就行
proorck6 2012-12-27
  • 打赏
  • 举报
回复
结构体嵌套结构体肯定没问题。
  • 打赏
  • 举报
回复
转化为字符串类型后,数据之间用个#号或者*号什么的隔开就行 至于结构体嵌套,注意格式肯定没问题
赵4老师 2012-12-27
  • 打赏
  • 举报
回复
不知道有多少前人掉在TCP Socket send(人多)send(病少)send(财富) recv(人多病)recv(少财富) 陷阱里面啊! http://topic.csdn.net/u/20120210/09/51109ed0-07b9-41f2-b487-a51597f2ca01.html
赵4老师 2012-12-27
  • 打赏
  • 举报
回复
结构体→xml文本→打包发送→收包缓冲→从缓冲中解包→xml文本→结构体
zhcosin 2012-12-27
  • 打赏
  • 举报
回复
引用 16 楼 liyapeng90 的回复:
引用 13 楼 zhcosin 的回复:另外我怀疑你的那个 SecurityID 恐怕应该字符串吧,咋让你在结构中搞成了 char ?应该是 char * 才对。那个是1个字节么,就弄了个字符,如果是字符串是不是前面那两个函数都要修改啊
如果是字符串当然要改啊,char 只有一个字符的,怎么改应该就简单了,你自己改一下吧。
zhcosin 2012-12-27
  • 打赏
  • 举报
回复
有两个小错误,更正了一下,通过了测试:

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

typedef struct M2M_HEAD
{
	int size;	
	int cmdid;	
	int	seqid;	
	int versionnum;	
	char SecurityID;
	char ReserveWord;
	int terminalID;	

};

int M2M_HEAD_to_str(char **dest, struct M2M_HEAD *pHead)
{
	unsigned int size = 0;

	if (dest == NULL || pHead == NULL)
		return -1;

	size = 128 + 5 * sizeof(int) + 2 *sizeof(char);

	*dest = (char *)malloc(size);
	if (*dest == NULL)
		return -2;

	memset(*dest, 0, size);

	sprintf(*dest,
		"size=%d, "
		"cmdid=%d, "
		"seqid=%d, "
		"versionnum=%d, "
		"SecurityID=%c, "
		"ReserveWord=%c, "
		"terminalID=%d"
		, pHead->size, pHead->cmdid, pHead->seqid, pHead->versionnum, 
		pHead->SecurityID, pHead->ReserveWord, pHead->terminalID);

	return 0;
}

int M2M_HEAD_parse(char *str, struct M2M_HEAD **pHead)
{
	if (str == NULL || pHead == NULL)
		return -1;

	*pHead = (struct M2M_HEAD *)malloc(sizeof(struct M2M_HEAD));
	if (*pHead == NULL)
		return -2;

	memset(*pHead, 0, sizeof(struct M2M_HEAD));

	sscanf(str, 
		"size=%d, "
		"cmdid=%d, "
		"seqid=%d, "
		"versionnum=%d, "
		"SecurityID=%c, "
		"ReserveWord=%c, "
		"terminalID=%d"
		, &((*pHead)->size), &((*pHead)->cmdid), &((*pHead)->seqid), &((*pHead)->versionnum), 
		&((*pHead)->SecurityID), &((*pHead)->ReserveWord), &((*pHead)->terminalID));

	return 0;
}

int main(int argc, char *argv[])
{
	struct M2M_HEAD head_a;
	struct M2M_HEAD *p = NULL;
	char *str = NULL;
	int res;

	head_a.size = 3;
	head_a.cmdid = 467;
	head_a.seqid = 246327542;
	head_a.versionnum = 33;
	head_a.SecurityID = 'w';
	head_a.ReserveWord = 'a';
	head_a.terminalID = 2644636;

	res = M2M_HEAD_to_str(&str, &head_a);
	if (res != 0)
	{
		printf("Failed to execute M2M_HEAD_to_str");
		return -1;
	}

	res = M2M_HEAD_parse(str, &p);
	if (res != 0)
	{
		free(str);
		str = NULL;
	}

	free(p);
	p = NULL;

	return 0;
}
liyapeng90 2012-12-27
  • 打赏
  • 举报
回复
引用 13 楼 zhcosin 的回复:
另外我怀疑你的那个 SecurityID 恐怕应该字符串吧,咋让你在结构中搞成了 char ?应该是 char * 才对。
那个是1个字节么,就弄了个字符,如果是字符串是不是前面那两个函数都要修改啊
liyapeng90 2012-12-27
  • 打赏
  • 举报
回复
引用 8 楼 zhcosin 的回复:
干脆都给你写了吧,你需要注意这里都是有内存分配的,用完了记得释放。 C/C++ code?123456789101112131415161718192021222324int M2M_HEAD_parser(char *str, struct M2M_HEAD **pHead){ if (str == NULL || pHead == NULL) r……
好了 那个MEMSET应该把*phead里面的至0,这样就不会崩了,万分感谢
zhcosin 2012-12-27
  • 打赏
  • 举报
回复
引用 11 楼 liyapeng90 的回复:
引用 8 楼 zhcosin 的回复:干脆都给你写了吧,你需要注意这里都是有内存分配的,用完了记得释放。 C/C++ code?123456789101112131415161718192021222324int M2M_HEAD_parser(char *str, struct M2M_HEAD **pHead){ if (str == NULL || pHea……
这大概是你外部调用的时候参数没传正确。
zhcosin 2012-12-27
  • 打赏
  • 举报
回复
另外我怀疑你的那个 SecurityID 恐怕应该字符串吧,咋让你在结构中搞成了 char ?应该是 char * 才对。
zhcosin 2012-12-27
  • 打赏
  • 举报
回复
引用 11 楼 liyapeng90 的回复:
引用 8 楼 zhcosin 的回复:干脆都给你写了吧,你需要注意这里都是有内存分配的,用完了记得释放。 C/C++ code?123456789101112131415161718192021222324int M2M_HEAD_parser(char *str, struct M2M_HEAD **pHead){ if (str == NULL || pHea……
哪个函数出的错?
liyapeng90 2012-12-27
  • 打赏
  • 举报
回复
引用 8 楼 zhcosin 的回复:
干脆都给你写了吧,你需要注意这里都是有内存分配的,用完了记得释放。
C/C++ code?123456789101112131415161718192021222324int M2M_HEAD_parser(char *str, struct M2M_HEAD **pHead){ if (str == NULL || pHead == NULL) r……

这个不可以啊执行到sscanf就出现这个了
liyapeng90 2012-12-27
  • 打赏
  • 举报
回复
引用 8 楼 zhcosin 的回复:
干脆都给你写了吧,你需要注意这里都是有内存分配的,用完了记得释放。 C/C++ code?123456789101112131415161718192021222324int M2M_HEAD_parser(char *str, struct M2M_HEAD **pHead){ if (str == NULL || pHead == NULL) r……
left of '->seqid' must point to struct/union 这个错误怎么改啊,恕小弟愚昧
liyapeng90 2012-12-27
  • 打赏
  • 举报
回复
引用 7 楼 zhcosin 的回复:
给你写一个从结构到字符串的吧,至于从字符串反解析你就自己做吧。 C/C++ code?12345678910111213141516171819202122232425262728int MWM_HEAD_to_str(char **dest, struct M2M_HEAD *pHead){ unsigned int size = 0; if (dest……
谢了,可以用,不过应该是_snprintf这个函数,不然少一杠连接错误,无法解析的外部符号 _snprintf,该符号在函数 _MWM_HEAD_to_str 中被引用
zhcosin 2012-12-27
  • 打赏
  • 举报
回复
干脆都给你写了吧,你需要注意这里都是有内存分配的,用完了记得释放。

int M2M_HEAD_parser(char *str, struct M2M_HEAD **pHead)
{
	if (str == NULL || pHead == NULL)
		return -1;

	*pHead = (struct M2M_HEAD *)malloc(sizeof(struct M2M_HEAD));
	if (*pHead == NULL)
		return -2;

	memset(pHead, 0, sizeof(struct M2M_HEAD));

	sscanf(str, 
		"size=%d, "
		"cmdid=%d, "
		"seqid=%d, "
		"versionnum=%d, "
		"SecurityID=%c, "
		"ReserveWord=%c, "
		"terminalID=%d"
		, &(*pHead->size), &(*pHead->cmdid), &(*pHead->seqid), &(*pHead->versionnum), 
		&(*pHead->SecurityID), &(*pHead->ReserveWord), &(*pHead->terminalID));

	return 0;
}
zhcosin 2012-12-27
  • 打赏
  • 举报
回复
给你写一个从结构到字符串的吧,至于从字符串反解析你就自己做吧。

int MWM_HEAD_to_str(char **dest, struct M2M_HEAD *pHead)
{
	unsigned int size = 0;

	if (dest == NULL || pHead == NULL)
		return -1;

	size = 128 + 5 * sizeof(int) + 2 *sizeof(char);

	*dest = (char *)malloc(size);
	if (*dest == NULL)
		return -2;

	memset(*dest, 0, size);

	snprintf(*dest, size, 
		"size=%d, "
		"cmdid=%d, "
		"seqid=%d, "
		"versionnum=%d, "
		"SecurityID=%c, "
		"ReserveWord=%c, "
		"terminalID=%d"
		, pHead->size, pHead->cmdid, pHead->seqid, pHead->versionnum, 
		pHead->SecurityID, pHead->ReserveWord, pHead->terminalID);

	return 0;
}
zhcosin 2012-12-27
  • 打赏
  • 举报
回复
你需要写两个函数,分别实现从结构体到字符串的文本化,以及从字符串到结构的解析。
加载更多回复(2)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧