Hex to int conversion error

A

Adam Ritter

When I try to convert an 8 digit hex number to an integer, I get a
ValueError. Why doesn't it convert back correctly? I have the string
'0xdeadbeaf' stored in a textbox and I would like it's integer value. I
would convert it to a long, but I need to pack it to send as a 4 byte
integer through a socket to a C program. Any ideas?
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: int() literal too large: 0xdeadbeaf

Nick

_________________________________________________________________
Enjoy MSN 8 patented spam control and more with MSN 8 Dial-up Internet
Service. Try it FREE for one month! http://join.msn.com/?page=dept/dialup
 
P

Peter Hansen

Adam said:
When I try to convert an 8 digit hex number to an integer, I get a
ValueError. Why doesn't it convert back correctly? I have the string
'0xdeadbeaf' stored in a textbox and I would like it's integer value. I
would convert it to a long, but I need to pack it to send as a 4 byte
integer through a socket to a C program. Any ideas?

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: int() literal too large: 0xdeadbeaf

If your description above, that you "need to pack it to send as a
4 byte integer", is correct, you should need only the struct module:

struct.pack('L', long('deadbeef', 16))

The value 0xdeadbeef is negative if treated as an int (since ints are
signed in Python), so you can't treat it as an unsigned int. Instead,
since in effect you want to treat all values as unsigned, use long()
and the "L" (unsigned long) operand to struct.pack. Note that if you
then give it a negative long, you'll still get an OverflowError,
this time from struct.pack itself.

-Peter
 
J

John Roth

Adam Ritter said:
When I try to convert an 8 digit hex number to an integer, I get a
ValueError. Why doesn't it convert back correctly? I have the string
'0xdeadbeaf' stored in a textbox and I would like it's integer value. I
would convert it to a long, but I need to pack it to send as a 4 byte
integer through a socket to a C program. Any ideas?

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: int() literal too large: 0xdeadbeaf

Nick

Please see PEP 237. If the timeline in that PEP is still valid,
the meaning will change in Release 2.4.

John Roth
 
P

Patrick Maupin

Adam Ritter said:
When I try to convert an 8 digit hex number to an integer, I get a
ValueError. Why doesn't it convert back correctly? I have the string
'0xdeadbeaf' stored in a textbox and I would like it's integer value. I
would convert it to a long, but I need to pack it to send as a 4 byte
integer through a socket to a C program. Any ideas?

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: int() literal too large: 0xdeadbeaf

Unfortunately, that's what you get in 2.2, which is _different_
than what you'll get in 2.3, which is _still_ _different_ than
what you'll get in 2.4. This whole "deal with machine words"
is not really directly supported -- even using struct as
one previous poster suggested can lead to trouble.

The solution is to write your own 'hex' and 'int' functions.
The following code **should** work under 2.2, 2.3, and 2.4,
although it will give FutureWarnings under 2.3 (you can shut
them off using the filter in the warnings module).

import sys


def short(what,offset=sys.maxint+1,modulus=(sys.maxint+1)*2):
"""short(n) converts a long into an int, ignoring high-order bits"""
return int(((what + offset) % modulus) - offset)

def poslong(what,modulus=(sys.maxint+1)*2):
"""poslong(n) takes an int and returns a long with the same bit
pattern in the lower bits, and zeros in the upper bits. (Guaranteed
positive result)"""
return what % modulus

def hexshort(what):
"""hexshort(n) returns a hex number without any annoying minus sign in 2.4"""
return '0x%x' % poslong(what)

print short(0xdeadbeef)
print hexshort(short(0xdeadbeef))
print long(hexshort(short(0xdeadbeef)),16)
print short(long(hexshort(short(0xdeadbeef)),16))

Hope this helps.

Pat
 
P

Patrick Maupin

Adam Ritter said:
When I try to convert an 8 digit hex number to an integer, I get a
ValueError. Why doesn't it convert back correctly? I have the string
'0xdeadbeaf' stored in a textbox and I would like it's integer value. I
would convert it to a long, but I need to pack it to send as a 4 byte
integer through a socket to a C program. Any ideas?

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: int() literal too large: 0xdeadbeaf


In my previous post, I forgot to mention that the code sample
I gave only gives FutureWarnings because of the literal hex
constant used. In your real application, you will not need
to do this (because you will be using long(somestring,16),
similar to the third test line in my example). So the chances
are that you will not encounter any FutureWarnings using this
method.

Pat
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top