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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robbie

    #1

    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


  • Raymond Hettinger

    #2
    Re: Is there the same function in python as atoi() in C


    "Robbie" <robbie_cao@msn .com> wrote in message
    news:bgfcfc$131 3$1@mail.cn99.c om...[color=blue]
    > 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[/color]

    int() should do the trick:
    [color=blue][color=green][color=darkred]
    >>> s = "81"
    >>> int(s)[/color][/color][/color]
    81


    Raymond Hettinger


    Comment

    • John Machin

      #3
      Re: Is there the same function in python as atoi() in C

      On Sat, 2 Aug 2003 11:51:54 +0800, "Robbie" <robbie_cao@msn .com>
      wrote:
      [color=blue]
      >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
      >
      >[/color]

      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('dem o_atoi argument (%s) contains
      non-digit(s)' % astr)
      return num

      Comment

      • Bengt Richter

        #4
        Re: Is there the same function in python as atoi() in C

        On Fri, 01 Aug 2003 20:55:42 -0700, washu <washu@127.0.0. 1> wrote:
        [color=blue]
        >On Sat, 02 Aug 2003 11:51:54 +0800, Robbie wrote:
        >[color=green]
        >> 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[/color]
        >
        >I may be totally wrong but I believe there is a atoi in the strings
        >modules.[/color]
        Easy to check:
        [color=blue][color=green][color=darkred]
        >>> import string
        >>> help(string.ato i)[/color][/color][/color]
        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

        Comment

        Working...