AnsweredAssumed Answered

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

Question asked by Ricardo Malerba on Apr 16, 2009
Latest reply on Apr 28, 2009 by Ricardo Malerba

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

Outcomes