473,546 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dumb noob q: ASCII value of a character

How can I get the ASCII value of a character, e.g. I have:

str = "A B"
for index in range(0, len(str)):
value = WHAT_GOES_HERE? (str[index])
print value
and I hope to get:
65
32
66
I know the characters will be printable ASCII.
But how do I get those ASCII character values from the characters???
I can't seem to find it in the library reference

Thanks,
Steve
Jul 18 '05 #1
4 6169
Steve Horsley wrote:

How can I get the ASCII value of a character, e.g. I have:

str = "A B"
for index in range(0, len(str)):
value = WHAT_GOES_HERE? (str[index])
print value

and I hope to get:
65
32
66

I know the characters will be printable ASCII.
But how do I get those ASCII character values from the characters???
I can't seem to find it in the library reference


It's a good idea to read through all the builtin functions, as
documented in http://www.python.org/doc/current/li...built-in-funcs
and keep them in mind.

In this case, ord() (and it's inverse chr()) are what you want.

-Peter
Jul 18 '05 #2

"Steve Horsley" <sh***@the.moon > schrieb im Newsbeitrag
news:c1******** **@news.freedom 2surf.net...
How can I get the ASCII value of a character, e.g. I have:

str = "A B"
for index in range(0, len(str)):
value = WHAT_GOES_HERE? (str[index])
print value
and I hope to get:
65
32
66
Hi Steve,

a word of advice: do not use the name of an existing function as identifier
for
a variable ( I am referring to your usage of str as the name of the
variable - you are reassigning it!!!). But back to your question:
What you're looking for is:

s = "A B"
for c in s:
print ord(c)

HTH,

Vincent Wehren


know the characters will be printable ASCII. But how do I get those ASCII character values from the characters???
I can't seem to find it in the library reference

Thanks,
Steve

Jul 18 '05 #3
* Steve Horsley <sh***@the.moon > [2004-02-26 11:28]:
How can I get the ASCII value of a character, e.g. I have:

str = "A B"
for index in range(0, len(str)):
value = WHAT_GOES_HERE? (str[index])
print value
and I hope to get:
65
32
66


ord.

(And yes, it's a little hard to find. The first time I had to
solve this problem, I used:
import string
string.index(st ring._idmap,'A' ) 65

)

Also of note: Strings can be used as iterators, so there's no need to
use that indexing trick:
str = "A B"
for char in str: .... print ord(char)
....
65
32
66

-John
< my_first_name AT my_last_name DOT net >

Jul 18 '05 #4
vincent wehren wrote:
What you're looking for is:

s = "A B"
for c in s:
print ord(c)

Thank you everyone who answered.

Also, "for c in s:" - hmmm , still learning fast!
Also, overriding str funcion with a local variable - and STILL learning fast!

Regards,
Steve
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8695
by: Haas | last post by:
Hello folks, I just started a C++ course. An exercise is to give the ASCII value of a character read from keyboard. I place the input from the keyboard in a char-variable. How can i get the ASCII value of this char? Is there a built-in function? Or should i check the full table? Thanks. Jasper
5
2494
by: lkrubner | last post by:
I know I'm missing something obvious, but I looked hard at this page and did not see the format of the return specified: http://us3.php.net/manual/en/function.ord.php >From the limited example I assume it is the decimal (not hex or binary) value of the character being returned? So long as the returned value is between 0 and 127, I can...
37
10118
by: chandy | last post by:
Hi, I have an Html document that declares that it uses the utf-8 character set. As this document is editable via a web interface I need to make sure than high-ascii characters that may be accidentally entered are properly represented when the document is served. My programming language allows me to get the ascii value for any individual...
3
32045
by: Director - Minvent | last post by:
Hi, I am reading from a serial port from a device which sends over an ascii character. It uses the full extended 256 character set and therefore has non-printing characters too. So what i want to do is convert the input to the associated decimal. I.E ASCII Char 'NUL' will become 0 ASCII Char 'SOH' will become 1
1
2989
by: Kermit Piper | last post by:
Hello, I have a function that lets me convert one character and throw an alert with the corresponding ASCII value, but what I am having trouble with is applying it to a text box. What I'm trying to do is when characters are entered the function will loop through the entered values and throw an alert for each of the corresponding ASCII...
4
25044
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. Anybody please help in c language. Thanks in Advance.
5
5421
by: tushar.saxena | last post by:
This post is a follow up to the post at : http://groups.google.com/group/comp.lang.c++/browse_thread/thread/83af6123fa945e8b?hl=ug#9eaa6fab5622424e as my original question was answered there, but I have some additional problems now. Basically what I want to do is : Given an input UTF-8 encoded file containing HTML sequences such as "&amp;", I...
9
3885
by: =?Utf-8?B?UGhhbmlkaGFy?= | last post by:
Hi, I'm developing a Winform application in C#( .net 2.0). I've a dialog box where user can input text and that text would be sent across to other machine using sockets. When the user enters ASCII character which are non-printable like ASCII 20 ( using ALT+20), this character is converted to ASCII value 194( or something like that). What...
9
4123
by: =?Utf-8?B?RGFu?= | last post by:
I have the following code section that I thought would strip out all the non-ascii characters from a string after decoding it. Unfortunately the non-ascii characters are still in the string. What am I doing wrong? Dim plainText As String plainText = "t═e" Dim plainTextBytes() As Byte Dim enc As Encoding = Encoding.ASCII...
0
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7794
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.