Problem with sprintf and zero padding [CodeWarrior 6.1 for V1 core ]

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem with sprintf and zero padding [CodeWarrior 6.1 for V1 core ]

Jump to solution
3,278 Views
Richard777
Contributor I

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 !

Message Edited by Richard777 on 2009-04-16 05:24 PM
Labels (1)
0 Kudos
1 Solution
714 Views
CompilerGuru
NXP Employee
NXP Employee

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

 

View solution in original post

0 Kudos
4 Replies
715 Views
CompilerGuru
NXP Employee
NXP Employee

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

 

0 Kudos
714 Views
Richard777
Contributor I

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 !

0 Kudos
714 Views
HummingbirdElec
Contributor I

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.

Message Edited by Hummingbird Electronics on 2009-04-28 11:13 AM
0 Kudos
714 Views
Richard777
Contributor I

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 !

0 Kudos