将unsigned char *附加到unsigned char *

时间:2015-03-19 22:57:04

标签: c concatenation itoa

我想将unsigned char *附加到另一个unsigned char *以创建一个可以返回父函数并进行解析的长字符串。我有以下内容:

unsigned char *bytes = NULL;
unsigned char *retVal = NULL;
int intVal1, intVal2;

bytes = readRegister(0x01); 
if ( bytes != NULL )
{
   intVal1 = (*(int *)bytes) & 0xffffffff;
   printf("intVal1 = 0x%x\n", intVal1); //intVal1 = 0x7fffac00
 }
bytes = readRegister(0x02); 
if ( bytes != NULL )
{
   intVal2 = (*(int *)bytes) & 0xffffffff;
   printf("intVal2 = 0x%x\n", intVal2); //intVal2 = 0x7fffac11
 }

如何连接intVal1和intVal2的值使retVal等于“7fffac007fffac11”?或者有没有办法在每次调用readRegister()之后将“bytes”连接到retVal?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

您想分配一些内存并使用snprintf来生成返回值。 调用者将负责使用free

取消分配返回值
retVal = malloc(17);
if (retVal) snprintf(retVal, 17, "%08x%08x", intVal1, intVal2);
return retVal;