How to covert ASCII to integer in Python?

Discussion in 'Python' started by hg, Feb 22, 2007.

  1. hg

    hg Guest

    John wrote:

    > Is there any built in function that converts ASCII to integer or vice
    > versa in Python?
    >
    > Thanks!
    >>> int('10')

    10
    >>> str(10)

    '10'
    >>>
     
    hg, Feb 22, 2007
    #1
    1. Advertising

  2. hg

    hg Guest

    <yours and that of others trying to guess>

    Some people spend many buck bying guessing games ... be nice !

    hg
     
    hg, Feb 22, 2007
    #2
    1. Advertising

  3. hg

    John Guest

    Is there any built in function that converts ASCII to integer or vice versa
    in Python?

    Thanks!
     
    John, Feb 22, 2007
    #3
  4. hg

    Larry Bates Guest

    John wrote:
    > Is there any built in function that converts ASCII to integer or vice versa
    > in Python?
    >
    > Thanks!
    >
    >

    You probably should go through the tutorial ASAP that is located here:

    http://docs.python.org/tut/


    Convert ascii string to integer:

    a='1'
    b=int(a)

    Convert integer to ascii string:

    a=1
    b=str(a)

    or

    a=1
    b="%i" % a

    -Larry Bates
     
    Larry Bates, Feb 22, 2007
    #4
  5. hg

    keirr Guest

    On Feb 22, 5:43 pm, "John" <> wrote:
    > Is there any built in function that converts ASCII to integer or vice versa
    > in Python?
    >
    > Thanks!


    Try int.
    ie.

    try:
    int_val = int(str_val)
    except ValueError:
    # conversion failed

    Keir.

    --
    Keir Robinson
    Sometimes a scream is better than a thesis. (Emerson)
     
    keirr, Feb 22, 2007
    #5
  6. hg

    John Guest

    I just found ord(c), which convert ascii to integer.

    Anybody know what the reverse is?

    "John" <> wrote in message
    news:erkknl$6d4p$...
    > Is there any built in function that converts ASCII to integer or vice

    versa
    > in Python?
    >
    > Thanks!
    >
    >
     
    John, Feb 22, 2007
    #6
  7. On Thu, 22 Feb 2007 12:43:43 -0500, "John" <> declaimed
    the following in comp.lang.python:

    > Is there any built in function that converts ASCII to integer or vice versa
    > in Python?
    >

    Magic 8-ball: The future is cloudy

    Do you mean converting a string representation of a numeric value to
    the binary numeric data type, and back...

    >>> a = "135"
    >>> b = "3.141592654"
    >>> c = "A"
    >>> i = int(a)
    >>> f = float(b)
    >>> print i, f

    135 3.141592654
    >>> str(i)

    '135'
    >>> str(f)

    '3.141592654'
    >>> "%s %s" % (i, f)

    '135 3.141592654'
    >>> "%5d %14.7f" % (i, f)

    ' 135 3.1415927'
    >>> "%-5d %14.7e" % (i, f)

    '135 3.1415927e+000'

    .... OR do you mean taking a single character and converting to the
    numeric value representing that character in the ASCII encoding...

    >>> i2 = ord(c)
    >>> i2

    65
    >>> chr(i2)

    'A'
    >>>

    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Feb 22, 2007
    #7
  8. hg

    Larry Bates Guest

    John wrote:
    > I just found ord(c), which convert ascii to integer.
    >
    > Anybody know what the reverse is?
    >
    > "John" <> wrote in message
    > news:erkknl$6d4p$...
    >> Is there any built in function that converts ASCII to integer or vice

    > versa
    >> in Python?
    >>
    >> Thanks!
    >>
    >>

    >
    >

    The phrasing of your question threw us all. What you want is chr

    backslash=chr(92)

    -Larry Bates
     
    Larry Bates, Feb 22, 2007
    #8
  9. hg

    Lloyd Zusman Guest

    "John" <> writes:

    > I just found ord(c), which convert ascii to integer.
    >
    > Anybody know what the reverse is?


    The inverse of "ord" is "chr":

    % python
    Python 2.5 (r25:51908, Jan 5 2007, 00:12:45)
    [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> ord('i')

    105
    >>> chr(105)

    'i'
    >>>


    IIRC, the first use of the names "ord" and "chr" for these functions
    appeared in the Basic language in the 1960's ... in case anyone is
    interested in this bit of historical trivia.


    --
    Lloyd Zusman

    God bless you.
     
    Lloyd Zusman, Feb 22, 2007
    #9
  10. hg

    Paul Rubin Guest

    "John" <> writes:
    > I just found ord(c), which convert ascii to integer.
    > Anybody know what the reverse is?


    chr(i)
     
    Paul Rubin, Feb 22, 2007
    #10
  11. hg

    MRAB Guest

    On Feb 22, 6:35 pm, Lloyd Zusman <> wrote:
    > "John" <> writes:
    > > I just found ord(c), which convert ascii to integer.

    >
    > > Anybody know what the reverse is?

    >
    > The inverse of "ord" is "chr":
    >
    > % python
    > Python 2.5 (r25:51908, Jan 5 2007, 00:12:45)
    > [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    > >>> ord('i')

    > 105
    > >>> chr(105)

    > 'i'
    > >>>

    >
    > IIRC, the first use of the names "ord" and "chr" for these functions
    > appeared in the Basic language in the 1960's ... in case anyone is
    > interested in this bit of historical trivia.
    >

    In the versions of Basic that I've seen they were ASC (clearly ASCII)
    and CHR$. I first saw ord in Pascal.
     
    MRAB, Feb 22, 2007
    #11
  12. hg

    John Machin Guest

    On Feb 23, 5:23 am, "John" <> wrote:
    > I just found ord(c), which convert ascii to integer.


    ord('\xff') -> 255
    ord(unichr(666)) -> 666

    What ascii?

    What is stopping you from reading the documentation section on built-
    in functions (http://docs.python.org/lib/built-in-funcs.html)?

    That way, you might find an answer to whatever your question really
    is, without wasting time (yours and that of others trying to guess).

    >
    > Anybody know what the reverse is?
    >
    > "John" <> wrote in message
    >
    > news:erkknl$6d4p$...
    >
    > > Is there any built in function that converts ASCII to integer or vice

    > versa
    > > in Python?

    >
    > > Thanks!
     
    John Machin, Feb 23, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    4
    Views:
    491
    Andrew Thompson
    Jul 29, 2005
  2. =?Utf-8?B?aGVuaw==?=

    Covert UTC String to UTC Datetime

    =?Utf-8?B?aGVuaw==?=, May 7, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    18,749
    Egbert Nierop \(MVP for IIS\)
    May 7, 2006
  3. Øystein Western

    read numbers from file and covert to integer

    Øystein Western, Jan 9, 2005, in forum: Python
    Replies:
    2
    Views:
    1,083
  4. Skonieczny, Chris

    How to covert ASCII to integer in Python?

    Skonieczny, Chris, May 30, 2008, in forum: Python
    Replies:
    3
    Views:
    551
    Philipp Pagel
    May 31, 2008
  5. Joshua Kugler
    Replies:
    2
    Views:
    277
    Mensanator
    May 31, 2008
Loading...

Share This Page