I'm rewriting some code to be compatible with 32-bit and 64-bit architectures, and I'm having trouble with a vsnprintf call. It doesn't appear that vsnprintf handles the fixed size integer types from inttypes.h properly on either architecture.
Here is the relevant code:
void formatString(char *buffer, int size, char *format, ...)
{
va_list va;
/* Format the data */
va_start( va, format );
vsnprintf( (char *)buffer, size, format, va );
va_end( va );
}
int main(int argc, char *argv[])
{
char buffer[2048];
printf("The format string: %s\n", stringsLookup(0));
formatString(&buffer[0], sizeof(buffer), stringsLookup(0), 1, 2);
printf("The output string: %s\n", buffer);
return 0;
}
The output is as follows:
The format string: action=DoSomething&Val1=%"PRIx32"&Val2=%x
The output string: action=DoSomething&Val1=%"PRIx32"&Val2=1
You can see that the %"PRIx32" portion of the format string was not replaced with the value '1' as expected. Is this a known issue? Is there a work around?
I will mention that if I hard code the strings in the source, the preprocessor appears to convert "%PRIu32" to the appropriate macro for the architecture and the call to vsnprintf works. Unfortunately I need to be able to load the strings.
Update
Some additional background: When I moved from a 32-bit system to a 64-bit system, I had to fix the size of certain variables. I declared them as uint32_t. I also changed the places where they were printed to clean up compiler warnings. The previous code used printf("%lx")
. I used printf("%"PRIx32)
. I need to do something similar with the call to vsnprintf.
As I've mentioned, if I hard code the string in the source code, the preprocessor appears to convert "%"PRIx32 to "%lx" or "%x" appropriately. Unfortunately, I'm running into trouble when I have to load the strings from a file. The preprocessor can't help me there.
stringsLookup(0)
? – Pascal Cuoq May 23 '14 at 20:10