473,806 Members | 2,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python 2.2 string conversion ?

ken
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
Jul 18 '05 #1
4 11164
On Thu, 24 Jul 2003 05:31:17 GMT, "ken" <ke********@ear thlink.net> wrote:
I've been looking for a solution to a string to long conversion problem that
I've run into
x = 'e10ea210'
print xe10ea210 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 x0xe10ea210 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('0xe10ea21 0',16)

3775832592L
Regards,
Bengt Richter
Jul 18 '05 #2
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 e10ea210y=long(x)

How about:
y = long(x, 16)

Jul 18 '05 #3
>
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("0xe10ea21 0",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

Jul 18 '05 #4
ken
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" <gh*****@island training.com> wrote in message
news:ma******** *************** *********@pytho n.org...
>> 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("0xe10ea21 0",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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
7092
by: srijit | last post by:
Hello, I would like to know the definition of type safe and whether Python can be considered as a type safe language. Similarly are Java, C# or C++ type safe? Regards, Srijit
10
2239
by: Willem | last post by:
When I run the follwing code using Python 2.3: from time import clock t1 = clock () for i in range (10000): a = int ('bbbbaaaa', 16) t2 = clock () for i in range (10000): a = long ('bbbbaaaa', 16) t3 = clock () print (t2-t1) / (t3-t2)
2
3640
by: geskerrett | last post by:
In the '80's, Microsoft had a proprietary binary structure to handle floating point numbers, In a previous thread, Bengt Richter posted some example code in how to convert these to python floats; http://groups.google.com/group/comp.lang.python/browse_thread/thread/42150ccc20a1d8d5/4aadc71be8aeddbe#4aadc71be8aeddbe I copied this code and modified it slightly, however, you will notice that for one of the examples, the conversion isn't...
2
4457
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
15
2356
by: Claudio Grondi | last post by:
Let's consider a test source code given at the very end of this posting. The question is if Python allows somehow access to the bytes of the representation of a long integer or integer in computers memory? Or does Python hide such implementation details that deep, that there is no way to get down to them? The test code below shows, that extracting bits from an integer value n is faster when using n&0x01 than when using n%2 and I...
0
278
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 430 open ( -4) / 3447 closed (+17) / 3877 total (+13) Bugs : 922 open ( -7) / 6316 closed (+31) / 7238 total (+24) RFE : 245 open ( +0) / 241 closed ( +1) / 486 total ( +1) New / Reopened Patches ______________________
1
1988
by: gabriel.becedillas | last post by:
I have a lot of functions returning "const std::string&". Every time I wrap one of those I have to do it like this: class_.def("name", &someclass::bla, boost::python::return_value_policy<boost::python::copy_const_reference>() ); Is there a way to register a return value conversion for "const std::string&" so I can omit it every time I have to wrap functions returning "const std::string&" ? I wan't those strings to be copied to
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10109
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5545
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4328
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3849
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.