[Tutor] built-in hex->int question

Lloyd Hugh Allen lha2@columbia.edu
Mon, 07 Jan 2002 21:41:33 -0500


Forgot to read the doc strings:

>>> print float.__doc__
float(x) -> floating point number

Convert a string or number to a floating point number, if possible.
>>> print long.__doc__
long(x) -> long integer
long(x, base) -> long integer

Convert a string or number to a long integer, if possible.  A floating
point argument will be truncated towards zero (this does not include a
string representation of a floating point number!)  When converting a
string, use the given base.  It is an error to supply a base when
converting a non-string.
>>> 

apparently float() is more forgiving than long(), or alternatively, long
wants to be told ahead of time when it is given a string that is either
not an integer or not base-10. Which makes sense, since number theorists
(and other folks who deal in Z (integers)) tend to be like that too. No
offense intended.

Lloyd Hugh Allen wrote:
> 
> You can do long(float('0xaf')), if you want to...don't know if that
> helps.
> 
> Huuuuuu wrote:
> >
> > Hi Tutor,
> >
> > Why understands the float() function a string argument but not
> > integer-cast-functions?
> >
> > >>> int('0xaf')
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > ValueError: invalid literal for int(): 0xaf
> > >>> long('0xaf')
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > ValueError: invalid literal for long(): 0xaf
> > >>> float('0xaf')
> > 175.0
> >
> > Thanks,
> > willi
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor