How to covert ASCII to integer in Python?

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

    How to covert ASCII to integer in Python?

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

    Thanks!


  • Larry Bates

    #2
    Re: How to covert ASCII to integer in Python?

    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:

    Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax an...



    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

    Comment

    • keirr

      #3
      Re: How to covert ASCII to integer in Python?

      On Feb 22, 5:43 pm, "John" <rds1...@sh163. netwrote:
      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)


      Comment

      • hg

        #4
        Re: How to covert ASCII to integer in Python?

        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'
        >>>

        Comment

        • John

          #5
          Re: How to covert ASCII to integer in Python?

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

          Anybody know what the reverse is?

          "John" <rds1226@sh163. netwrote in message
          news:erkknl$6d4 p$1@netnews.upe nn.edu...
          Is there any built in function that converts ASCII to integer or vice
          versa
          in Python?
          >
          Thanks!
          >
          >

          Comment

          • Larry Bates

            #6
            Re: How to covert ASCII to integer in Python?

            John wrote:
            I just found ord(c), which convert ascii to integer.
            >
            Anybody know what the reverse is?
            >
            "John" <rds1226@sh163. netwrote in message
            news:erkknl$6d4 p$1@netnews.upe nn.edu...
            >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(9 2)

            -Larry Bates

            Comment

            • Paul Rubin

              #7
              Re: How to covert ASCII to integer in Python?

              "John" <rds1226@sh163. netwrites:
              I just found ord(c), which convert ascii to integer.
              Anybody know what the reverse is?
              chr(i)

              Comment

              • Lloyd Zusman

                #8
                Re: How to covert ASCII to integer in Python?

                "John" <rds1226@sh163. netwrites:
                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
                ljz@asfast.com
                God bless you.

                Comment

                • MRAB

                  #9
                  Re: How to covert ASCII to integer in Python?

                  On Feb 22, 6:35 pm, Lloyd Zusman <l...@asfast.co mwrote:
                  "John" <rds1...@sh163. netwrites:
                  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.

                  Comment

                  • John Machin

                    #10
                    Re: How to covert ASCII to integer in Python?

                    On Feb 23, 5:23 am, "John" <rds1...@sh163. netwrote:
                    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" <rds1...@sh163. netwrote in message
                    >
                    news:erkknl$6d4 p$1@netnews.upe nn.edu...
                    >
                    Is there any built in function that converts ASCII to integer or vice
                    versa
                    in Python?
                    >
                    Thanks!

                    Comment

                    • hg

                      #11
                      Re: How to covert ASCII to integer in Python?


                      <yours and that of others trying to guess>

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

                      hg

                      Comment

                      Working...