Problem in zero padding?

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

Problem in zero padding?

1,494 Views
mohd_
Contributor III

Hi, 

I have problem with zero padding.

Here is my example code. I am using MCUXpresso IDE.

        PRINTF("0x%04X  ",  buf);

The result is in the picture attached.

Best regards,

Syamil

 

キャプチャ3.PNG

0 Kudos
4 Replies

1,452 Views
diego_charles
NXP TechSupport
NXP TechSupport

Hi @mohd_ 

In this case, this is the behaviour of the PRINTF function.

if the number for the left padding is greather than the number to be printed, the PRINTF function will pad blank spaces.

Please, take a look at the below snapshot:

padding.png

https://mcuxpresso.nxp.com/api_doc/dev/116/group__debugconsole.html

I hope this could help you!

If you have any comment of further question, let me know!

All the best,

Diego

 

 

 

0 Kudos

1,419 Views
mohd_
Contributor III

Hi @diego_charles ,

Thank you for the information!

0 Kudos

1,446 Views
jforslof
Contributor II

Hello Diego,

That is true the row above in the table states that a 0 prefix replaces space with zero padding and the format give was "%02X", as would be the case with standard C printf formatting.

Might this be a size optimization option to trimming down the set of formatting variants supported?

@mohd_: Have you considered rolling your own simple zero-extended tracing function as a workaround? Below is one such straightforward and entirely untested implementation 

 

void PrintZeroExtended(uint32_t value, uint32_t radix, int32_t width) {
  char buffer[sizeof "01234567"];
  char *textptr = &buffer[sizeof buffer];
  *--textptr = '\0';
  do {
    *--textptr = "0123456789ABCDEF"[value % radix];
    value /= radix;
  } while(--width >= 0 || value);
  PRINTF("%s", textptr);
}

 

Regards,
Johan

0 Kudos

1,418 Views
mohd_
Contributor III

Hi Johan @jforslof ,

Thank you for your suggestion!

Your code works well!

Regards,

Syamil

キャプチャ4.PNG

0 Kudos