By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,272 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,272 IT Pros & Developers. It's quick & easy.

How to covert ASCII to integer in Python?

P: n/a
Is there any built in function that converts ASCII to integer or vice versa
in Python?

Thanks!
Feb 22 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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

Feb 22 '07 #2

P: n/a
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)
Feb 22 '07 #3

P: n/a
hg
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'
>>>
Feb 22 '07 #4

P: n/a
I just found ord(c), which convert ascii to integer.

Anybody know what the reverse is?

"John" <rd*****@sh163.netwrote in message
news:er***********@netnews.upenn.edu...
Is there any built in function that converts ASCII to integer or vice
versa
in Python?

Thanks!


Feb 22 '07 #5

P: n/a
John wrote:
I just found ord(c), which convert ascii to integer.

Anybody know what the reverse is?

"John" <rd*****@sh163.netwrote in message
news:er***********@netnews.upenn.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(92)

-Larry Bates
Feb 22 '07 #6

P: n/a
"John" <rd*****@sh163.netwrites:
I just found ord(c), which convert ascii to integer.
Anybody know what the reverse is?
chr(i)
Feb 22 '07 #7

P: n/a
"John" <rd*****@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
lj*@asfast.com
God bless you.

Feb 22 '07 #8

P: n/a
On Feb 22, 6:35 pm, Lloyd Zusman <l...@asfast.comwrote:
"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.

Feb 22 '07 #9

P: n/a
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:er***********@netnews.upenn.edu...
Is there any built in function that converts ASCII to integer or vice
versa
in Python?
Thanks!

Feb 23 '07 #10

P: n/a
hg

<yours and that of others trying to guess>

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

hg

Feb 23 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.