Hi guys, I have problems with sprintf and zero padding, look this example:
... char szTemp[80]; unsigned int Written; Written = 1; sprintf( szTemp, "%02u", Written + 1 );...
The result in szTemp array is the following:
szTemp[0] = 0x31;
szTemp[1] = 0x00;
The zero padding does not seem to work, also I looked the source file printf_tiny_io.c ( source file for printf/sprintf functions ) and I have found the problem.
/*** Line number 653 ****/ /* pad left */ if (width && !(flags & _PRINTF_LEFT)) { if (!(flags & _PRINTF_ZEROPAD)) {/* pad with spaces */ do { out(' '); --width; } while (width); } else {#if LIBDEF_PRINTF_PREC prec += width; #endif width = 0; /* pad with zeros */ } }
The left pad work only with space !. My corrections to these:
/* pad left */ if (width && !(flags & _PRINTF_LEFT)) { if (!(flags & _PRINTF_ZEROPAD)) {/* pad with spaces */ do { out(' '); --width; } while (width); } else {#if LIBDEF_PRINTF_PREC prec += width; #endif /* pad with zeros */ do { out('0'); --width; } while (width); } }
Sorry for my poor english and best regards !
已解决! 转到解答。
Hi Richard,
The fix is fine if LIBDEF_PRINTF_PREC is set to 0, but if it is enabled (it is not for CF, it is for HC08/HC12) it causes the added zeros to be dublicated.
Here's a fixed version:
/* pad left */ if (width && !(flags & _PRINTF_LEFT)) { if (!(flags & _PRINTF_ZEROPAD)) {/* pad with spaces */ do { out(' '); --width; } while (width); } else { /* pad with zeros */#if LIBDEF_PRINTF_PREC prec += width; width = 0;#else do { out('0'); --width; } while (width);#endif } }
As alternative for your fix, you could also set LIBDEF_PRINTF_PREC to 1 for CF too.
Daniel
Hi Richard,
The fix is fine if LIBDEF_PRINTF_PREC is set to 0, but if it is enabled (it is not for CF, it is for HC08/HC12) it causes the added zeros to be dublicated.
Here's a fixed version:
/* pad left */ if (width && !(flags & _PRINTF_LEFT)) { if (!(flags & _PRINTF_ZEROPAD)) {/* pad with spaces */ do { out(' '); --width; } while (width); } else { /* pad with zeros */#if LIBDEF_PRINTF_PREC prec += width; width = 0;#else do { out('0'); --width; } while (width);#endif } }
As alternative for your fix, you could also set LIBDEF_PRINTF_PREC to 1 for CF too.
Daniel
Hi Daniel, thanks for your responce. I think that the first solution is better because the define LIBDEF_PRINT_PREC refer to precision no to padding, look the following note in the CodeWarrior help:
#define LIBDEF_PRINTF_PREC (1 || LIBDEF_PRINTF_FLOATING)
Set to 1 if '.' (precision support) in printf/scanf is needed.
Best regards !
Thanks - have spent hours fighting this. So, as a "don't fiddle with the libraries" type of user, I have changed the .c file - now how do I force the compiler to recompile and link the libraries?
I tried to load hc08_lib.mcp and compile it, but my compiler is limited to 32k and so it failed.
Hi, copy the file printf_tiny_IO.c located in the <Program Files\Freescale\CodeWarrior for Microcontrollers V6.1\ColdFire_Support\msl\MSL_C\MSL_Common\Src> into your project, modify it and then compile. You must ensure that this file is the first in the link order.
Best regards !