Convert text to ASCII and ASCII to text - Python code
Python has a very simple way to convert text to ASCII and ASCII to text.
To find the ASCII value of a character use the ord() function.
Example:
>>> ord('B')
66
To get a character from it's ASCII value use the chr() function.
Example:
>>> chr(65)
'A'
Here is a program to get the list of all characters along with their values:
Here is another simple ASCII encoding-decoding program:
Hope you got a clear understanding of how to convert between text and ASCII code.
To find the ASCII value of a character use the ord() function.
Example:
>>> ord('B')
66
To get a character from it's ASCII value use the chr() function.
Example:
>>> chr(65)
'A'
Here is a program to get the list of all characters along with their values:
# program to print the list of characters and their ASCII values
for value in range(0, 255):
print "ASCII Value:", value, "\t", "Character:", chr(value), "\n"
Here is another simple ASCII encoding-decoding program:
# Convertion from text to ASCII codes
message = raw_input("Enter message to encode: ")
print "Decoded string (in ASCII):"
for ch in message:
print ord(ch),
print "\n\n"
# Convertion from ASCII codes to text
message = raw_input("Enter ASCII codes: ")
decodedMessage = ""
for item in message.split():
decodedMessage += chr(int(item))
print "Decoded message:", decodedMessage
Hope you got a clear understanding of how to convert between text and ASCII code.
Comments
I used to use chr() and ord() to store huge numbers (like 512bit integers) by bit-shifting by 8 and &ing. Actually I used '%c' % 65, but I think it's pretty much the same as chr(65).
for char in range(256):
print "%d: %c" % (char, char)
I started to learn Python a week ago. I'm glad that I found your blog, it'll be useful for me.
>>> [ord(c) for c in u'ó'.encode('utf-8')]
[194, 179]
On the other hand:
>>> '%c%c' % (194, 179)
'\xc2\xb3'
>>> 'ó'
'\xc3\xb3'
Can you tell me, why '%c%c' % (194, 179) != 'ó'? And why:
>>> print '%c%c' % (195, 179)
'ó'
Bests,
Attila
May be because %c %c took the default code page of the underlying stream which is why we did not get ó.
I just took a wild-guess.. Correct me if I am wrong.
>>> import unicodedata
>>> unicodedata.lookup('LEFT CURLY BRACKET')
u'{'
>>> unicodedata.name(u'/')
'SOLIDUS'
>>> unicodedata.decimal(u'9')
9
>>> unicodedata.decimal(u'a')
Traceback (most recent call last):
File "", line 1, in ?
ValueError: not a decimal
>>> unicodedata.category(u'A') # 'L'etter, 'u'ppercase
'Lu'
>>> unicodedata.bidirectional(u'\u0660') # 'A'rabic, 'N'umber
'AN'
like i type... 'raja' it cnvert as राजा