binary representaion of a number, debug

Alex Martelli alex at magenta.com
Mon Aug 21 07:56:24 EDT 2000


"David Broadwell" <dbroadwell at mindspring.com> wrote in message
news:39A0DBD0.1BB15F44 at mindspring.com...
> First, thanks for the suggestions. Second i have run ito a problem that
> i am not sure how to deal with. I am importing the hexidecimal data from
> a file, preserving the addresses and converting the hex to binary,
> writing back out to a companion file.
>
> In the process of this i get the error message:
> Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> import string
> >>> line = string.atoi('ade80000',16)
> Traceback (innermost last):
>   File "<pyshell#18>", line 1, in ?
>     line = string.atoi('ade80000',16)
> ValueError: atoi() literal too large: ade80000
>
> While ade90000 is the largest value in my sample file, i'd like to be
> able to support from 00000000 to ffffffff. Any suggestions for how to
> deal with this?

string.atoi() wants to return a number as positive, unless it was
preceded by a minus-sign, and so it finds a 32-number starting with
a 1 in the most-significant bit "too large" on a 32-bit machine.

You can use string.atol(), which returns a long-integer (an integral
number with no predefined limits as to the "length" in bits it can
have).  There are, of course, other approaches, but string.atol()
is likely to be the easiest/handiest one for your needs.


Alex






More information about the Python-list mailing list