String to Hex
Michael Foord
fuzzyman at gmail.com
Fri Oct 29 03:42:39 EDT 2004
Rick Holbert <holbertr at dma.org> wrote in message news:<closr5$4vs$1 at charm.magnus.acs.ohio-state.edu>...
> The following look like they will work on Python 2.3.4:
>
> long(float('-0x55aff8080000'))
>>> long(float('-0x55aff8080000'))
Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
long(float('-0x55aff8080000'))
ValueError: invalid literal for float(): -0x55aff8080000
>>>
It was this error that surprised me......
> int(float('-0x55aff8080000'))
> eval('-0x55aff8080000')
eval doesn't get round the problem at all....
>>> eval('0xffffffff')
<string>:0: FutureWarning: hex/oct constants > sys.maxint will return
positive values in Python 2.4 and up
-1
Regards,
Fuzzy
http://www.voidspace.org.uk/atlantibots/pythonutils.html
>
> Some programmers frown upon the use of eval(), so use it at your own risk.
>
> Comments?
>
> Rick
>
> Michael Foord wrote:
>
> > How do you safely turn an arbitrarily long signed hex string into a
> > long integer ?
> > e.g. -0x55aff8080000
> >
> > When treating them as hex literals I'm getting future warnings that
> > from 2.4 hex values greater than sys.maxint will always return
> > positive values. I've had to treat them as strings and write a
> > function to turn them into longs a character at a time ! I couldn't
> > find a built in function that would do this for me.
> >
> > Regards,
> >
> > Fuzzy
> >
> > http://www.voidspace.org.uk/atlantibots/pythonutils.html
More information about the Python-list
mailing list