Integer as raw hex string?

Discussion in 'Python' started by Roy Smith, Dec 24, 2012.

  1. Roy Smith

    Roy Smith Guest

    I have an integer that I want to encode as a hex string, but I don't
    want "0x" at the beginning, nor do I want "L" at the end if it happened
    to be a long. The result needs to be something I can pass to int(h, 16)
    to get back my original integer.

    The brute force way works:

    h = hex(i)
    assert h.startswith('0x')
    h = h[2:]
    if h.endswith('L'):
    h = h[:-1]

    but I'm wondering if there's some built-in call which gives me what I
    want directly. Python 2.7.
    Roy Smith, Dec 24, 2012
    #1
    1. Advertising

  2. Roy Smith

    Tim Chase Guest

    On 12/24/12 09:36, Roy Smith wrote:
    > I have an integer that I want to encode as a hex string, but I don't
    > want "0x" at the beginning, nor do I want "L" at the end if it happened
    > to be a long. The result needs to be something I can pass to int(h, 16)
    > to get back my original integer.
    >
    > The brute force way works:
    >
    > h = hex(i)
    > assert h.startswith('0x')
    > h = h[2:]
    > if h.endswith('L'):
    > h = h[:-1]
    >
    > but I'm wondering if there's some built-in call which gives me what I
    > want directly. Python 2.7.


    Would something like

    h = "%08x" % i

    or

    h = "%x" % i

    work for you?

    -tkc
    Tim Chase, Dec 24, 2012
    #2
    1. Advertising

  3. Roy Smith

    Roy Smith Guest

    In article <>,
    Tim Chase <> wrote:

    > On 12/24/12 09:36, Roy Smith wrote:
    > > I have an integer that I want to encode as a hex string, but I don't
    > > want "0x" at the beginning, nor do I want "L" at the end if it happened
    > > to be a long. The result needs to be something I can pass to int(h, 16)
    > > to get back my original integer.
    > >
    > > The brute force way works:
    > >
    > > h = hex(i)
    > > assert h.startswith('0x')
    > > h = h[2:]
    > > if h.endswith('L'):
    > > h = h[:-1]
    > >
    > > but I'm wondering if there's some built-in call which gives me what I
    > > want directly. Python 2.7.

    >
    > Would something like
    >
    > h = "%08x" % i
    >
    > or
    >
    > h = "%x" % i
    >
    > work for you?


    Duh. Of course. Thanks.
    Roy Smith, Dec 24, 2012
    #3
  4. Roy Smith

    MRAB Guest

    On 2012-12-24 15:58, Tim Chase wrote:
    > On 12/24/12 09:36, Roy Smith wrote:
    >> I have an integer that I want to encode as a hex string, but I don't
    >> want "0x" at the beginning, nor do I want "L" at the end if it happened
    >> to be a long. The result needs to be something I can pass to int(h, 16)
    >> to get back my original integer.
    >>
    >> The brute force way works:
    >>
    >> h = hex(i)
    >> assert h.startswith('0x')
    >> h = h[2:]
    >> if h.endswith('L'):
    >> h = h[:-1]
    >>
    >> but I'm wondering if there's some built-in call which gives me what I
    >> want directly. Python 2.7.

    >
    > Would something like
    >
    > h = "%08x" % i
    >
    > or
    >
    > h = "%x" % i
    >
    > work for you?
    >

    Or:

    h = "{:x}".format(i)
    MRAB, Dec 24, 2012
    #4
    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. Replies:
    10
    Views:
    6,144
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    450
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    576
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    18,803
  5. tim
    Replies:
    2
    Views:
    1,544
    Dennis Lee Bieber
    Nov 23, 2005
Loading...

Share This Page