473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
Jul 18 '05 #1
3 42371

"Robbie" <ro********@msn.com> wrote in message
news:bg***********@mail.cn99.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
Jul 18 '05 #2
On Sat, 2 Aug 2003 11:51:54 +0800, "Robbie" <ro********@msn.com>
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

Jul 18 '05 #3
On Fri, 01 Aug 2003 20:55:42 -0700, washu <wa***@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
Jul 18 '05 #4

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

Similar topics

2
by: Robin Becker | last post by:
I'm getting the following error when I attempt to run an app with Python 2.3. I suppose this was silently ignored under 2.2. With debugging I see that s==0 in _int = int ...... def atoi(s ,...
4
by: Alex Vinokur | last post by:
Hi, I need something like "function inheritance". typedef void (*func_type1) (int); typedef int (*func_type2) (double); typedef char (*func_type3) (short, int); .... I need a vector...
6
by: Allan Bruce | last post by:
Hi there, I am loading in a 3d object from file. The code I have works fine but it is very slow for objects with many vertices, i.e. >5000 I make use of the atoi function 3 times for each vertex,...
5
by: Bansidhar | last post by:
atoi() function seems not to have any support for Hex, octal number. Usually when I read from a text file then it contain number like 0x232 etc. In this case atoi() fells. In case of itoa() there...
15
by: Anonymousgoogledeja | last post by:
Hi all, since the function atof, atoi, _atoi64, atol returned value are Return Values Each function returns the double, int, __int64 or long value produced by interpreting the input...
9
by: zxo102 | last post by:
Hi everyone, I am using a python socket server to collect data from a socket client and then control a image location ( wxpython) with the data, i.e. moving the image around in the wxpython frame....
6
by: mdh | last post by:
I guess pointers just take time!!! This is exercise 5-13, peripherally. The function accepts a command line argument of -n. So, given int main ( int argc, char *argv)
4
by: digitnctu | last post by:
Dear all: I am coming with problem, to apply ctypes under interactive mode in python. libdll.dll is a third-party library. The below code segment will run well under the batch mode(ie. python...
4
by: Ram | last post by:
Hi All, Firstly i am a newbie and trying to learn C. The background of the problem is Program: Presently I am working on a program of numerology and the I/P will be the name and output...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.