How to covert ASCII to integer in Python?

H

hg

<yours and that of others trying to guess>

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

hg
 
J

John

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

Thanks!
 
L

Larry Bates

John said:
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
 
K

keirr

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.
 
J

John

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

Anybody know what the reverse is?
 
D

Dennis Lee Bieber

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...
'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...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
L

Larry Bates

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

Anybody know what the reverse is?
The phrasing of your question threw us all. What you want is chr

backslash=chr(92)

-Larry Bates
 
L

Lloyd Zusman

John said:
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.
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.
 
M

MRAB

John said:
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.
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top