String to Hex
Rick Holbert
holbertr at dma.org
Fri Oct 29 09:18:11 EDT 2004
Interesting!
Here are the results from my Linux Workstation (Mandrake 10.1 on Intel):
Python 2.3.4 (#2, Aug 19 2004, 15:49:40)
[GCC 3.4.1 (Mandrakelinux (Alpha 3.4.1-3mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> long(float('-0x55aff8080000'))
-94214268911616L
Here are the results from my school's Solaris Server (Solaris 8 on Sparc):
Python 2.3.4 (#1, Sep 24 2004, 17:33:58)
[GCC 3.0.4] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> long(float('-0x55aff8080000'))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for float(): -0x55aff8080000
Also, here are the results from Jython on the same server:
Jython 2.1 on java1.4.0_01 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> long(float('-0x55aff8080000'))
Traceback (innermost last):
File "<console>", line 1, in ?
ValueError: invalid literal for __float__: -0x55aff8080000
Michael Foord wrote:
> 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