Re: Using %x to format number to hex and number of digits

Discussion in 'Python' started by Tim Chase, Nov 5, 2010.

  1. Tim Chase

    Tim Chase Guest

    On 11/05/10 13:23, Matty Sarro wrote:
    >> I'm currently trying to convert a digit from decimal to hex,
    >> however I need the full 4 digit hex form. Python appears to
    >> be shortening the form.
    >> Example:
    >>
    >> num = 10
    >> num = "%x"%(num)
    >> print(num)
    >>
    >>>> a

    >>
    >> num = 10
    >> num = "%#x"%(num)
    >> print(num)
    >>
    >>>> 0xa

    >>
    >> I need it to output as 0x0a, and the exercise is requiring
    >> me to use %x to format the string. Any help would be
    >> appreciated.


    Though it feels hokey to me, using

    "%#04x" % 10

    works for me. The "#" adds the "0x" prefix (the "alternate
    form"), the "0" pads with zeros, and the "4" is really "2 places
    + 2 characters for the prefix". So if you want 4 hex characters
    + the 2 prefix characters, you'd use "%#06x". IMHO, this would
    make more sense if it didn't take the 2-character prefix into
    consideration (being written as "%#02x" for a byte or "%#04x" for
    a short), but it's not really a burr in my saddle.

    -tkc
     
    Tim Chase, Nov 5, 2010
    #1
    1. Advertising

  2. On 2010-11-05, Tim Chase <> wrote:
    > On 11/05/10 13:23, Matty Sarro wrote:
    >>> I'm currently trying to convert a digit from decimal to hex,
    >>> however I need the full 4 digit hex form. Python appears to
    >>> be shortening the form.
    >>> Example:
    >>>
    >>> num = 10
    >>> num = "%x"%(num)
    >>> print(num)
    >>>
    >>>>> a
    >>>
    >>> num = 10
    >>> num = "%#x"%(num)
    >>> print(num)
    >>>
    >>>>> 0xa
    >>>
    >>> I need it to output as 0x0a, and the exercise is requiring
    >>> me to use %x to format the string. Any help would be
    >>> appreciated.

    >
    > Though it feels hokey to me, using
    >
    > "%#04x" % 10
    >
    > works for me.


    I think "0x%02x" % 10 is a bit more readable, but it accomplishes the
    same thing -- you just don't have to do the mental math to add the
    prefix width to the number of desired hex digits in the output.

    --
    Grant Edwards grant.b.edwards Yow! Quick, sing me the
    at BUDAPEST NATIONAL ANTHEM!!
    gmail.com
     
    Grant Edwards, Nov 5, 2010
    #2
    1. Advertising

  3. Tim Chase

    Tim Chase Guest

    On 11/05/10 15:10, Grant Edwards wrote:
    > On 2010-11-05, Tim Chase<> wrote:
    >> On 11/05/10 13:23, Matty Sarro wrote:
    >>>> I need it to output as 0x0a, and the exercise is requiring
    >>>> me to use %x to format the string. Any help would be
    >>>> appreciated.

    >>
    >> Though it feels hokey to me, using
    >>
    >> "%#04x" % 10
    >>
    >> works for me.

    >
    > I think "0x%02x" % 10 is a bit more readable, but it accomplishes the
    > same thing -- you just don't have to do the mental math to add the
    > prefix width to the number of desired hex digits in the output.


    Which, I confess, is what I usually use because I didn't know
    about the "#" until this thread, and just made the assumption
    that one can get the zero-padding along with the "alternate form"
    in my experimentation this afternoon.

    So while it _can_ be done with just variants of the "%x"
    place-holder (as suggested by the OP's exercise), at least two of
    us on the list prefer to explicitly add the "0x" to the beginning.

    -tkc
     
    Tim Chase, Nov 6, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aefxx
    Replies:
    1
    Views:
    616
    Virgil Green
    Dec 17, 2004
  2. Replies:
    10
    Views:
    6,395
    Neredbojias
    Aug 19, 2005
  3. Bengt Richter
    Replies:
    6
    Views:
    527
    Juha Autero
    Aug 19, 2003
  4. Chris Rebert
    Replies:
    1
    Views:
    414
    Grant Edwards
    Nov 5, 2010
  5. chirs
    Replies:
    3
    Views:
    191
    Dr John Stockton
    Dec 1, 2003
Loading...

Share This Page