hex oct int string coversions?

Discussion in 'Python' started by Ron Adam, Nov 3, 2003.

  1. Ron Adam

    Ron Adam Guest

    How do I covert a string hex or oct value to the correct int value?

    >>> s = oct(500)
    >>> s # returns a oct string

    '0764'

    >>> int(0764) # ok

    500

    >>> int(s) # this gives the wrong value

    764

    >>> t = hex(500)
    >>> t # returns a hex string

    '0x1f4'

    >>> int(0x1f4) # ok

    500

    >>> int(t) # causes an error

    Traceback (most recent call last):
    File "<pyshell#65>", line 1, in ?
    int(t)
    ValueError: invalid literal for int(): 0x1f4
     
    Ron Adam, Nov 3, 2003
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Nov 3, 2003
    #2
    1. Advertising

  3. Ron Adam

    Ron Adam Guest

    On Mon, 03 Nov 2003 03:41:16 +0100, "Martin v. Löwis"
    <> wrote:

    >Ron Adam wrote:
    >
    >> How do I covert a string hex or oct value to the correct int value?

    >
    >http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-41
    >
    >Martin




    That tells me how to convert an int to a hex or oct string using the
    hex() and oct() functions, which I already know.

    The int() function will convert a string to an integer. But it
    doesn't understand hex or oct from a string., You can type the hex,
    or oct value as an argument for the int() function on the command line
    and that works correctly.

    So there must be a way to do it? Am I missing a step?

    _Ron
     
    Ron Adam, Nov 3, 2003
    #3
  4. Ron Adam

    Ron Adam Guest

    On Mon, 03 Nov 2003 04:47:48 GMT, Ron Adam <>
    wrote:

    >On Mon, 03 Nov 2003 03:41:16 +0100, "Martin v. Löwis"
    ><> wrote:
    >
    >>Ron Adam wrote:
    >>
    >>> How do I covert a string hex or oct value to the correct int value?

    >>
    >>http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-41
    >>
    >>Martin

    >
    >
    >
    >That tells me how to convert an int to a hex or oct string using the
    >hex() and oct() functions, which I already know.
    >
    >The int() function will convert a string to an integer. But it
    >doesn't understand hex or oct from a string., You can type the hex,
    >or oct value as an argument for the int() function on the command line
    >and that works correctly.
    >
    >So there must be a way to do it? Am I missing a step?
    >
    >_Ron
    >



    It was there, I wasn't looking at it correctly. int(s,0) or
    int(s,8) works great. Thanks Martin. And also to Mike who pointed
    it out to me in email.

    I really need to get more sleep.

    _Ron
     
    Ron Adam, Nov 3, 2003
    #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,388
    Neredbojias
    Aug 19, 2005
  2. Schnoffos
    Replies:
    2
    Views:
    1,251
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,708
    Old Wolf
    Jan 20, 2004
  4. Bengt Richter
    Replies:
    6
    Views:
    525
    Juha Autero
    Aug 19, 2003
  5. Ram
    Replies:
    1
    Views:
    290
    Thad Smith
    Dec 2, 2005
Loading...

Share This Page