Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How to zero-pad hex values in sprintf()?

by Cody Fendant (Pilgrim)
on Jan 18, 2013 at 21:07 UTC ( #1014126=perlquestion: print w/ replies, xml ) Need Help??
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?

Comment on How to zero-pad hex values in sprintf()?
Select or Download Code
Re: How to zero-pad hex values in sprintf()?
by MidLifeXis (Prior) 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 (Abbot) 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 (Pilgrim) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1014126]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-10-26 01:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls