March 31st, 2005, 07:09 PM
-
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:
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?
March 31st, 2005, 08:26 PM
-
i don't know if this is the best way to do this but it works
Code:
>>>eval("0x"+'1234567890123'[:3])
291
March 31st, 2005, 08:39 PM
-
Thanks a lot jacktasia. This works just fine.
March 31st, 2005, 08:45 PM
-
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.
March 31st, 2005, 08:46 PM
-
Oh. That works too.