Velocity Reviews > How to covert ASCII to integer in Python?

# How to covert ASCII to integer in Python?

hg
Guest
Posts: n/a

 02-22-2007
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
Guest
Posts: n/a

 02-22-2007

<yours and that of others trying to guess>

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

hg

John
Guest
Posts: n/a

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

Thanks!

Larry Bates
Guest
Posts: n/a

 02-22-2007
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

keirr
Guest
Posts: n/a

 02-22-2007
On Feb 22, 5:43 pm, "John" <(E-Mail Removed)> 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)

John
Guest
Posts: n/a

 02-22-2007
I just found ord(c), which convert ascii to integer.

Anybody know what the reverse is?

"John" <(E-Mail Removed)> wrote in message
news:erkknl\$6d4p\$(E-Mail Removed)...
> Is there any built in function that converts ASCII to integer or vice

versa
> in Python?
>
> Thanks!
>
>

Dennis Lee Bieber
Guest
Posts: n/a

 02-22-2007
On Thu, 22 Feb 2007 12:43:43 -0500, "John" <(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/

Larry Bates
Guest
Posts: n/a

 02-22-2007
John wrote:
> I just found ord(c), which convert ascii to integer.
>
> Anybody know what the reverse is?
>
> "John" <(E-Mail Removed)> wrote in message
> news:erkknl\$6d4p\$(E-Mail Removed)...
>> 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

Lloyd Zusman
Guest
Posts: n/a

 02-22-2007
"John" <(E-Mail Removed)> 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
(E-Mail Removed)
God bless you.

Paul Rubin
Guest
Posts: n/a

 02-22-2007
"John" <(E-Mail Removed)> writes:
> I just found ord(c), which convert ascii to integer.
> Anybody know what the reverse is?

chr(i)