Home >Backend Development >Python Tutorial >How to convert ascii to characters in python

How to convert ascii to characters in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-19 17:10:146498browse

ASCII (American Standard Code for Information Interchange): American Standard Code for Information Interchange) is a computer coding system based on the Latin alphabet, mainly used to display modern English and other Western European languages. It is the most common information exchange standard and is equivalent to the international standard ISO/IEC 646. ASCII was first published as a standard type in 1967, and was last updated in 1986. So far, a total of 128 characters have been defined.

How to convert ascii to characters in python

ASCII code expression

ASCII code uses a specified 7-bit or 8-bit binary number combination to represent 128 or 256 possible characters. Standard ASCII code, also called basic ASCII code, uses 7 binary digits (the remaining 1 binary digit is 0) to represent all uppercase and lowercase letters, numbers 0 to 9, punctuation marks, and special controls used in American English. character. Among them:

0~31 and 127 (33 in total) are control characters or special communication characters (the rest are displayable characters), such as control characters: LF (line feed), CR (carriage return), FF ( Page feed), DEL (delete), BS (backspace), BEL (ring), etc.; communication special characters: SOH (head of text), EOT (end of text), ACK (confirmation), etc.; ASCII values ​​are 8, 9 , 10 and 13 are converted to backspace, tab, line feed and carriage return characters respectively. They do not have a specific graphic display, but will have different effects on text display depending on the application.

Related recommendations: "Python Video Tutorial"

32~126 (95 in total) are characters (32 is a space), of which 48~57 are 0 to 9 Ten Arabic numerals.

65~90 are 26 uppercase English letters, 97~122 are 26 lowercase English letters, and the rest are some punctuation marks, arithmetic symbols, etc.

Also note that in standard ASCII, its highest bit (b7) is used as a parity bit. The so-called parity check refers to a method used to check whether errors occur during code transmission. It is generally divided into two types: odd check and even check. Odd parity stipulates: the number of 1's in a byte of the correct code must be an odd number. If it is not an odd number, add 0 to the highest bit b7; even parity stipulates: the number of 1's in a byte of the correct code must be an even number. , if it is not an even number, add 1 to the highest bit b7.

The last 128 are called extended ASCII codes. Many x86-based systems support the use of extended (or "high") ASCII. Extended ASCII allows the 8th bit of each character to be used to determine additional 128 special symbol characters, foreign language letters, and graphic symbols.

Common ASCII code size rules: 0~9

1) Numbers are smaller than letters. Such as "7" < "F";

2) The number 0 is smaller than the number 9, and increases in sequence from 0 to 9. For example, "3" < "8";

3) The letter A is smaller than the letter Z, and increases in order from A to Z. For example, "A" < "Z";

4) The uppercase letters of the same letter are 32 smaller than the lowercase letters. Such as "A"<"a".

The ASCII code sizes of several common letters: "A" is 65; "a" is 97; "0" is 48.

The following code is used to convert ASCII codes and characters:

 # 用户输入字符
 c = input("请输入一个字符: ")
 # 用户输入ASCII码,并将输入的数字转为整型
 a = int(input("请输入一个ASCII码: "))
 
 print( c + " 的ASCII 码为", ord(c))
 print( a , " 对应的字符为", chr(a))

The output result of executing the above code is:

请输入一个字符: a
请输入一个ASCII码: 101
a 的ASCII 码为 97
101  对应的字符为 e

The above is the detailed content of How to convert ascii to characters in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn