user-0 提问: 求助用C拼接Buffer的问题，求高手指点？

回复:
user: user-0
content: 搞定啦，谢谢各位的支持

user: user-1
content: 仅仅根据你的结果，把前面两个数转换成字符就行了。 例如：   54 是 字符‘6’的十进制值。          50 50 是字符‘2’‘2’的十进制值。 你看看你哪个地方转错了。  引用 6 楼 absenvan 的回复:我用下面的方式打印buf：  for (int i = 0; i< (len1+len2+len3); i++) {             printf("%d,",buf[i]);         }  你的代码打印如下： 54,118,49,46,51,46,48,50,50,104,116,116,112,58,47,47,119,119,119,46,115,105,110,97,46,99,111,109,46,99,110,51,51,57,98,97,97,49,54,56,100,57,98,99,99,99,57,102,97,99,55,55,97,98,97,50,97,97,57,102,48,  我失望得到这样的： 0,6,118,49,46,51,46,48,0,22,104,116,116,112,58,47,47,119,119,119,46,115,105,110,97,46,99,111,109,46,99,110,0,33,57,98,97,97,49,54,56,100,57,98,99,99,99,57,102,97,99,55,55,97,98,97,50,97,97,57,102,48,55,55,100,99,51,   我解解释下： 0,6,  //第一个参数的长度 118,49,46,51,46,48, //第一个参数 0,22, //第二个参数的长度 104,116,116,112,58,47,47,119,119,119,46,115,105,110,97,46,99,111,109,46,99,110, //第二个参数 0,33,//第三个长度 57,98,97,97,49,54,56,100,57,98,99,99,99,57,102,97,99,55,55,97,98,97,50,97,97,57,102,48,55,55,100,99,51,//第三个参数  请问你上面的代码怎么优化得到这个数据呢？

user: user-2
content: malloc、realloc、free

user: user-0
content: Buffer  =  第一个参数+ 第二个参数 + 第三个参数 拼接格式： Buffer   =  [wParamLen + strParamValue]+ [wParamLen + strParamValue]+ [wParamLen + strParamValue]   wParamLen:表示2个字节，strParameValue：表示字符串。

user: user-0
content: 我用下面的方式打印buf：  for (int i = 0; i< (len1+len2+len3); i++) {             printf("%d,",buf[i]);         }  你的代码打印如下： 54,118,49,46,51,46,48,50,50,104,116,116,112,58,47,47,119,119,119,46,115,105,110,97,46,99,111,109,46,99,110,51,51,57,98,97,97,49,54,56,100,57,98,99,99,99,57,102,97,99,55,55,97,98,97,50,97,97,57,102,48,  我失望得到这样的： 0,6,118,49,46,51,46,48,0,22,104,116,116,112,58,47,47,119,119,119,46,115,105,110,97,46,99,111,109,46,99,110,0,33,57,98,97,97,49,54,56,100,57,98,99,99,99,57,102,97,99,55,55,97,98,97,50,97,97,57,102,48,55,55,100,99,51,   我解解释下： 0,6,  //第一个参数的长度 118,49,46,51,46,48, //第一个参数 0,22, //第二个参数的长度 104,116,116,112,58,47,47,119,119,119,46,115,105,110,97,46,99,111,109,46,99,110, //第二个参数 0,33,//第三个长度 57,98,97,97,49,54,56,100,57,98,99,99,99,57,102,97,99,55,55,97,98,97,50,97,97,57,102,48,55,55,100,99,51,//第三个参数  请问你上面的代码怎么优化得到这个数据呢？

user: user-3
content: 这样可好：  int main(void) { 	char buf[256]; 	char *p = buf; 	int r; 	char *para1 = "v1.3.0"; //strParamValue; 	unsigned short len1 = strlen(para1);  //wParamLen 	char *para2 = "http://www.sina.com.cn"; 	unsigned short len2 = strlen(para2); 	char *para3 = "9baa168d9bccc9fac77aba2aa9f077dc3"; 	unsigned short len3 = strlen(para3); 	r = sprintf(p, "%d", len1); 	p += r; 	r = sprintf(p, "%s", para1); 	p += r; 	r = sprintf(p, "%d", len2); 	p += r; 	r = sprintf(p, "%s", para2); 	p += r; 	r = sprintf(p, "%d", len3); 	p += r; 	r = sprintf(p, "%s", para3); 	printf("%s\n", buf); 	return 0; } //6v1.3.022http://www.sina.com.cn339baa168d9bccc9fac77aba2aa9f077dc3

user: user-4
content: char *para1 = "v1.3.0"; char *para2 = "http://www.sina.com.cn"; char *para3 = "9baa168d9bccc9fac77aba2aa9f077dc3"; int len = strlen(para1) + strlen(para2) + strlen(para3);  char *buffer = (char *)malloc(len + 1); sprintf(buffer, "%s%s%s", para1, para2, para3); printf("%s", buffer); free(buffer)

user: user-0
content: 这是我拼接的不知道对不对啊？求指导？         char *para1 = "v1.3.0";         char  len1 =  strlen(para1);         char *para2 = "http://www.sina.com.cn";         char  len2 =  strlen(para2);         char *para3 = "9baa168d9bccc9fac77aba2aa9f077dc3";         char  len3 =  strlen(para3);                  char buffer[256]={0};         strcat(buffer, &len1);         strcat(buffer, para1);                  strcat(buffer, &len2);         strcat(buffer, para2);                  strcat(buffer, &len3);         strcat(buffer, para3);         printf("%s",buffer);

user: user-5
content: strcat或sprintf