String to Hex

M

Michael Foord

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
 
P

Peter Otten

Michael said:
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.

One (fragile) way to avoid that Python 2.3 interprets
0x80000000...0xffffffff (on 32-bit systems) as negative integers is to add
an explicit sign:

$ python -c"print int('0xffffffff', 0)"
-c:1: FutureWarning: int('0...', 0): sign will change in Python 2.4
-1
$ python -c"print int('+0xffffffff', 0)"
4294967295
$ python -c"print int('-0xffffffff', 0)"
-4294967295

Peter
 
R

Rick Holbert

The following look like they will work on Python 2.3.4:

long(float('-0x55aff8080000'))
int(float('-0x55aff8080000'))
eval('-0x55aff8080000')

Some programmers frown upon the use of eval(), so use it at your own risk.

Comments?

Rick
 
P

Peter Otten

Looks good. Why is it fragile ?
(-2147483648, 2147483648L)

Dunno. Is it only me feeling the itch to remove the redundant-looking +
sign?
True

Is that the equality you want to bury somewhere in a script? The fact that
the int() argument probably is a variable rather than a string constant
makes it only worse.
I'd say there is one more reason to look forward to Python 2.4.

Peter
 
M

Michael Foord

Rick Holbert said:
The following look like they will work on Python 2.3.4:

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....<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
 
R

Rick Holbert

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.-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.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.Traceback (innermost last):
File "<console>", line 1, in ?
ValueError: invalid literal for __float__: -0x55aff8080000
 
O

Oliver Fromme

Michael Foord said:
> How do you safely turn an arbitrarily long signed hex string into a
> long integer ?
> e.g. -0x55aff8080000

I don't really see what the problem is.

$ python -c 'print int("-0x55aff8080000", 16)'
-94214268911616

Works fine on 2.3.4, no warning. The "0x" is optional,
so if your strings don't contain it from the beginning,
you don't have to insert it.

Best regards
Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top