http://www.perlmonks.org?node_id=1014126

Cody Fendant has asked for the wisdom of the Perl Monks concerning the following question:

It's of academic interest because I can always check length and add the zero myself but, am I crazy or do printf() and sprintf() have no way to return, say, 0F instead of just F?

Replies are listed 'Best First'.
Re: How to zero-pad hex values in sprintf()?
by MidLifeXis (Monsignor) on Jan 18, 2013 at 21:10 UTC

    printf("%04x\n", 254)

    --MidLifeXis

Re: How to zero-pad hex values in sprintf()?
by blue_cowdawg (Monsignor) on Jan 18, 2013 at 21:11 UTC
        am I crazy or do printf() and sprintf() have no way to return, say, 0F instead of just F?

    I'm not a psychiatrist and never played one on TV so I can't judge if you're crazy or not. But check out this sniglet of code.

    $ perl -e 'printf "%0.4x\n",128;' 0080


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: How to zero-pad hex values in sprintf()?
by AnomalousMonk (Archbishop) on Jan 18, 2013 at 21:52 UTC

    Also, please see sprintf for a comprehensive discussion of the formatting specifiers available.

Re: How to zero-pad hex values in sprintf()?
by Cody Fendant (Hermit) on Jan 25, 2013 at 22:45 UTC

    Thanks to everyone who posted. I feel rather foolish. Somehow I was adding the zero for the padding but missing the number. I need more sleep.