0

I'd like to append an unsigned char* to another unsigned char* to create a long string that can be returned to a parent function and parsed. I have the following:

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
 }

How can concatenate the values of intVal1 and intVal2 so retVal would equal "7fffac007fffac11"? Or is there a way to concatenate "bytes" to retVal after each call to readRegister()?

Any help is greatly appreciated!

txcotrader
  • 585
  • 1
  • 6
  • 21
  • can or can't be unaligned? This code seems to be doing what I want it to do without compiler errors. Could you please elaborate, I'd like to understand the problem? – txcotrader Mar 19 '15 at 23:06
  • Excuse me, the address of `bytes` **may** be unaligned for an `int`, [take a look](http://c-faq.com/strangeprob/ptralign.html) – David Ranieri Mar 19 '15 at 23:10

1 Answers1

2

You want to allocate some memory and use snprintf to produce your return value. The caller will be responsible for deallocating the return value with free:

retVal = malloc(17);
if (retVal) snprintf(retVal, 17, "%08x%08x", intVal1, intVal2);
return retVal; 
chqrlie
  • 131,814
  • 10
  • 121
  • 189