Thread: Int() Problem

    #1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Int() Problem


    I've having a bit of a problem doing something in Python that Visual Basic 6 has the capability of. I need Python to do:
    Code:
    int('0x' + '1234567890123'[:3])
    I just made up a number with the correct length. Python will return the following error:
    Code:
    Traceback (most recent call last):
      File "<pyshell#26>", line 1, in ?
        int('0x')
    ValueError: invalid literal for int(): 0x
    I want Python to return:
    Code:
    >>> 0x123
    291
    But, I can do:
    Code:
    >>> 0x+int('1234567890123'[:3])
    123
    The problem is that Python isn't returning 291. I don't already know what the number is going to be, so there's no way to automatically know the first three digits of it. Is there another way to doing this?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    Lawrence, Kansas [KU]
    Posts
    1,559
    Rep Power
    15
    i don't know if this is the best way to do this but it works

    Code:
    >>>eval("0x"+'1234567890123'[:3])
    291
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Thanks a lot jacktasia. This works just fine.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by †Yegg†
    The problem is that Python isn't returning 291. I don't already know what the number is going to be, so there's no way to automatically know the first three digits of it. Is there another way to doing this?
    int("123", 16)
    hex(291)

    --OH.
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Oh. That works too.

IMN logo majestic logo threadwatch logo seochat tools logo