Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python 2.2 string conversion ?

Reply
Thread Tools

python 2.2 string conversion ?

 
 
ken
Guest
Posts: n/a
 
      07-24-2003
I've been looking for a solution to a string to long conversion problem that
I've run into

>>> x = 'e10ea210'
>>> print x

e10ea210
>>> y=long(x)

Traceback (most recent call last):
File "<pyshell#2>", line 1, in ?
y=long(x)
ValueError: invalid literal for long(): e10ea210
>>> x='0xe10ea210'
>>> print x

0xe10ea210
>>> y=long(x)

Traceback (most recent call last):
File "<pyshell#5>", line 1, in ?
y=long(x)
ValueError: invalid literal for long(): 0xe10ea210
>>> x="e10ea210"
>>> y=long(x)

Traceback (most recent call last):
File "<pyshell#7>", line 1, in ?
y=long(x)
ValueError: invalid literal for long(): e10ea210
>>> x="0xe10ea210"
>>> y=long(x)

Traceback (most recent call last):
File "<pyshell#9>", line 1, in ?
y=long(x)
ValueError: invalid literal for long(): 0xe10ea210
>>>


What am I doing wrong?

TIA


 
Reply With Quote
 
 
 
 
Bengt Richter
Guest
Posts: n/a
 
      07-24-2003
On Thu, 24 Jul 2003 05:31:17 GMT, "ken" <(E-Mail Removed)> wrote:

>I've been looking for a solution to a string to long conversion problem that
>I've run into
>
>>>> x = 'e10ea210'
>>>> print x

>e10ea210
>>>> y=long(x)

>Traceback (most recent call last):
> File "<pyshell#2>", line 1, in ?
> y=long(x)
>ValueError: invalid literal for long(): e10ea210
>>>> x='0xe10ea210'
>>>> print x

>0xe10ea210
>>>> y=long(x)

>Traceback (most recent call last):
> File "<pyshell#5>", line 1, in ?
> y=long(x)
>ValueError: invalid literal for long(): 0xe10ea210
>>>> x="e10ea210"
>>>> y=long(x)

>Traceback (most recent call last):
> File "<pyshell#7>", line 1, in ?
> y=long(x)
>ValueError: invalid literal for long(): e10ea210
>>>> x="0xe10ea210"
>>>> y=long(x)

>Traceback (most recent call last):
> File "<pyshell#9>", line 1, in ?
> y=long(x)
>ValueError: invalid literal for long(): 0xe10ea210
>>>>

>
>What am I doing wrong?
>

Need to supply base if converting string that is not base 10

>>> long('123')

123L
>>> long('0xe10ea210',16)

3775832592L


Regards,
Bengt Richter
 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      07-24-2003
>
> >>> x="e10ea210"
> >>> y=long(x)

>
> Traceback (most recent call last):
> File "<pyshell#7>", line 1, in ?
> y=long(x)
> ValueError: invalid literal for long(): e10ea210


> What am I doing wrong?


You didn't specify what you are trying to do here, but I'll make a
wild *guess* that the string in x is a hexadecimal (i.e., base 16)
value. However, Python can't go around making such a guess, so you
have to explicitly specify your radix (radix being another term for
base) like this:

>>> print long("e10ea210",16)

3775832592

or tell it to infer the radix from a '0x' prefix:

>>> print long("0xe10ea210",0)

3775832592

Here are the relevant portions of the manual:

long(x[, radix])

Convert a string or number to a long integer. If the argument is a
string, it must contain a possibly signed number of arbitrary size,
possibly embedded in whitespace; this behaves identical to
string.atol(x). The radix argument is interpreted in the same way as
for int(), and may only be given when x is a string. Otherwise, the
argument may be a plain or long integer or a floating point number,
and a long integer with the same value is returned. Conversion of
floating point numbers to integers truncates (towards zero).


int(x[, radix])

Convert a string or number to a plain integer. If the argument is a
string, it must contain a possibly signed decimal number
representable as a Python integer, possibly embedded in whitespace;
this behaves identical to string.atoi(x[, radix]). The radix
parameter gives the base for the conversion and may be any integer
in the range [2, 36], or zero. If radix is zero, the proper radix is
guessed based on the contents of string; the interpretation is the
same as for integer literals. If radix is specified and x is not a
string, TypeError is raised. Otherwise, the argument may be a plain
or long integer or a floating point number. Conversion of floating
point numbers to integers truncates (towards zero). If the argument
is outside the integer range a long object will be returned instead.


Gary Herron



 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      07-24-2003
ken wrote:
> I've been looking for a solution to a string to long conversion problem that
> I've run into
>>>>x = 'e10ea210'
>>>>print x

> e10ea210
>>>>y=long(x)

>

How about:
y = long(x, 16)

 
Reply With Quote
 
ken
Guest
Posts: n/a
 
      07-24-2003
It wasn't clear to me when I read the docs - I inferred that the long()
built-in only took 1 parameter.

Thanks everybody.

"Gary Herron" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >
> > >>> x="e10ea210"
> > >>> y=long(x)

> >
> > Traceback (most recent call last):
> > File "<pyshell#7>", line 1, in ?
> > y=long(x)
> > ValueError: invalid literal for long(): e10ea210

>
> > What am I doing wrong?

>
> You didn't specify what you are trying to do here, but I'll make a
> wild *guess* that the string in x is a hexadecimal (i.e., base 16)
> value. However, Python can't go around making such a guess, so you
> have to explicitly specify your radix (radix being another term for
> base) like this:
>
> >>> print long("e10ea210",16)

> 3775832592
>
> or tell it to infer the radix from a '0x' prefix:
>
> >>> print long("0xe10ea210",0)

> 3775832592
>
> Here are the relevant portions of the manual:
>
> long(x[, radix])
>
> Convert a string or number to a long integer. If the argument is a
> string, it must contain a possibly signed number of arbitrary size,
> possibly embedded in whitespace; this behaves identical to
> string.atol(x). The radix argument is interpreted in the same way as
> for int(), and may only be given when x is a string. Otherwise, the
> argument may be a plain or long integer or a floating point number,
> and a long integer with the same value is returned. Conversion of
> floating point numbers to integers truncates (towards zero).
>
>
> int(x[, radix])
>
> Convert a string or number to a plain integer. If the argument is a
> string, it must contain a possibly signed decimal number
> representable as a Python integer, possibly embedded in whitespace;
> this behaves identical to string.atoi(x[, radix]). The radix
> parameter gives the base for the conversion and may be any integer
> in the range [2, 36], or zero. If radix is zero, the proper radix is
> guessed based on the contents of string; the interpretation is the
> same as for integer literals. If radix is specified and x is not a
> string, TypeError is raised. Otherwise, the argument may be a plain
> or long integer or a floating point number. Conversion of floating
> point numbers to integers truncates (towards zero). If the argument
> is outside the integer range a long object will be returned instead.
>
>
> Gary Herron
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion xhtml champs Python 0 06-21-2011 11:59 AM
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion PSD to XHTML Conversion Services and PSD to HTML CSS Conversion Services, PSD to Joomla, Drupal, Wor VHDL 0 04-25-2011 06:43 AM
conversion operator and conversion ctor subramanian100in@yahoo.com, India C++ 2 09-15-2009 12:46 PM
boost::python and automatic conversion for const std::string& gabriel.becedillas@gmail.com Python 1 12-21-2006 05:55 PM
data conversion question (binary string to 'real string') Alexander Eisenhuth Python 5 07-25-2003 06:42 PM



Advertisments