printf formats for uint8_t, etc, WITH LEADING ZEROS

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So <inttypes.h> defines macros for printf-style formats for printing the <stdint.h> types - uint8_t, etc.

 

https://en.wikibooks.org/wiki/C_Programming/inttypes.h

 

But there don't seem to be any for printing with leading zeros - eg, as with "%02X"

 

Am I missing something ... ?

 

 

EDIT

 

No, PRIX8 does not give leading zeros.

 

sad

This topic has a solution.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. May 21, 2018 - 04:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
Am I missing something ... ?

I think so.  IME, printf formats are usually described (what reference are you using?) something like:

The format specifier string has the following structure:

 

%[flags][width][.precision][l]type_char

 

The optional flags characters are:

      '-' left-justifies the result, padding on the right with spaces. If its not present, the result will be right-justified, padded on the left with zeros or spaces;

      '+' signed conversion results will always begin with a '+' or '-' sign;

      ' ' if the value isn't negative, the conversion result will begin with a space. If the value is negative then it will begin with a '-' sign.

 

The optional width specifier sets the minimal width of an output value. If the result of the conversion is wider than the field width, the field will be expanded to accommodate the result, so not to cause field truncation.

 

The following width specifiers are supported:

      n - at least n characters are outputted. If the result has less than n characters, then its field will be padded with spaces. If the '-' flag is used, the result field will be padded on the right, otherwise it will be padded on the left;

      0n - at least n characters are outputted. If the result has less than n characters, it is padded on the left with zeros.

 

The optional precision specifier sets the maximal number of characters or minimal number of integer digits that may be outputted. ...

 

The 0n is independent from the type_char (x or X in your example).

 

Trix are for kids.  PRIX are for quiche-eating politically-correct Generation-whatevers and not for real printf programmers.

http://www.qnx.com/developers/do...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Mon. May 21, 2018 - 04:23 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
No, PRIX8 does not give leading zeros.
The PRIX8 is only for the type. Additional formatting is done the "usual way":

"%02"PRIX8

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Isn't this why it's left for you to provide the %n bit ? The header only defines:

/** \ingroup avr_inttypes
    decimal printf format for uint8_t */
#define         PRIu8                   "u"

So presumably you can:

printf("%02" PRIu8, myu8Var);

EDIT: must learn to copy/paste faster!

 

Last Edited: Mon. May 21, 2018 - 04:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:
The PRIX8 is only for the type. Additional formatting is done the "usual way":

"%02"PRIX8

 

I thought I had tried that,  but now I see that I had just put "%0"PRIX8 - I'd missed the width number!

 

blush

 

Thanks to everyone for the answer - even though the forum only allows one to be marked as "the" (sic) solution.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
PRIX are for quiche-eating politically-correct Generation-whatevers and not for real printf programmers

 

laugh

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
laugh

I'm just going by what I learnt from a master a few years ago:

awneil wrote:
printf() is a power tool and, like any power tool, is great when used properly - but can be unwieldy or even dangerous when used inappropriately and/or incorrectly.

https://www.avrfreaks.net/forum/...

 

Oh, wait, that was you...

 

[edit] LOL -- another pertinent fragment from that thread:

wek3 wrote:
Real embedded programmers try to understand thoroughly all the implications including possible side effects of any library function (including printf()). This is supposed to be covered in painful details in the libraries'/toolchains' accompanying documentation. It is very, very rarely so. This why embedded programmers don't use printf().

 

And from a link in that thread:

Real programmers scorn floating point arithmetic. The decimal point was invented for pansy bedwetters who are unable to think big.

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Mon. May 21, 2018 - 05:17 PM