How to covert ASCII to integer in Python?

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

1. hgGuest

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

2. hgGuest

<yours and that of others trying to guess>

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

hg

hg, Feb 22, 2007

3. JohnGuest

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

Thanks!

John, Feb 22, 2007
4. Larry BatesGuest

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
5. keirrGuest

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
6. JohnGuest

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
7. Dennis Lee BieberGuest

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
8. Larry BatesGuest

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
9. Lloyd ZusmanGuest

"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
>>> 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
10. Paul RubinGuest

"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
11. MRABGuest

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
> >>> 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
12. John MachinGuest

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