Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is there the same function in python as atoi() in C

Reply
Thread Tools

Is there the same function in python as atoi() in C

 
 
Robbie
Guest
Posts: n/a
 
      08-02-2003
Someone tell me,
if not exist, how to write one with the same function?
I have made a try, but there is no character type in python,
so i failed, someone help me,
thanks very much


 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      08-02-2003

"Robbie" <(E-Mail Removed)> wrote in message
news:bgfcfc$1313$(E-Mail Removed)99.com...
> Someone tell me,
> if not exist, how to write one with the same function?
> I have made a try, but there is no character type in python,
> so i failed, someone help me,
> thanks very much


int() should do the trick:

>>> s = "81"
>>> int(s)

81


Raymond Hettinger


 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      08-02-2003
On Sat, 2 Aug 2003 11:51:54 +0800, "Robbie" <(E-Mail Removed)>
wrote:

>Someone tell me,
>if not exist, how to write one with the same function?
>I have made a try, but there is no character type in python,
>so i failed, someone help me,
>thanks very much
>
>


General question: Have you read some of the tutorials?

Regarding your specific problem:

1. Think about it. A language without such a function would be utterly
useless.

2. There is a built-in function called int() -- look it up in the
manual.

3. Don't bother with the atoi() in the string module -- inspection of
...../Lib/string.py will tell you that it just calls int() anyway.

4. No character type? How would you like that, signed or unsigned or
implementation-defined? Python avoids that little chamber of horrors
and does rather well by using strings of length 1. Here's a rough
untested no-negative-allowed no-explicit-overflow-checking
base-10-only atoi() ...

def demo_atoi(astr):
num = 0
for c in astr:
if '0' <= c <= '9':
num = num * 10 + ord(c) - ord('0')
else:
raise ValueError('demo_atoi argument (%s) contains
non-digit(s)' % astr)
return num

 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      08-02-2003
On Fri, 01 Aug 2003 20:55:42 -0700, washu <washu@127.0.0.1> wrote:

>On Sat, 02 Aug 2003 11:51:54 +0800, Robbie wrote:
>
>> Someone tell me,
>> if not exist, how to write one with the same function?
>> I have made a try, but there is no character type in python,
>> so i failed, someone help me,
>> thanks very much

>
>I may be totally wrong but I believe there is a atoi in the strings
>modules.

Easy to check:

>>> import string
>>> help(string.atoi)

Help on function atoi in module string:

atoi(s, base=10)
atoi(s [,base]) -> int

Return the integer represented by the string s in the given
base, which defaults to 10. The string s must consist of one
or more digits, possibly preceded by a sign. If base is 0, it
is chosen from the leading characters of s, 0 for octal, 0x or
0X for hexadecimal. If base is 16, a preceding 0x or 0X is
accepted.

Regards,
Bengt Richter
 
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
How can I use a class name Rect when there is a function has the same name yinglcs@gmail.com C++ 2 02-13-2007 12:39 AM
Is there a way to pass a python function ptr to a c++ method from a python script? liam_herron Python 1 06-06-2006 02:32 AM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
[python] Is there a python written fax program out there? David Stockwell Python 2 06-08-2004 08:28 PM



Advertisments