C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

ASCII Encoding, Store English To Computer

As we have mentioned before, the computer stores data in binary form. It only recognizes two numbers, 0 and 1. The text we see on the screen is converted into binary (0 and 1 sequence) before storage. ), and also find the corresponding character according to binary when displaying.

It is conceivable that a specific text must correspond to a fixed binary, otherwise confusion will occur during conversion. So, how do you map text to binary? This requires a set of specifications that computer companies and software developers must abide by. Such a set of specifications is called Character Set or Character Encoding.
Strictly speaking, character set and character encoding are not the same concept. Character set defines the correspondence between text and binary, assigns unique numbers to characters, and character encoding specifies how to store text numbers in the computer. We will not discuss these details for the time being, and consider them as one concept. In this section, I also use these two concepts interchangeably without making a distinction. The character set assigns a unique number to each character, similar to a student's student number, and the corresponding character can be found through the number.

The character set can be understood as a large table, which lists the correspondence between all characters and binary. The computer displays text or stores text, which is a process of looking up the table.

In the process of the gradual development of computers, dozens or even hundreds of character sets have appeared successively, some of which are still in use, and some have been submerged in the long river of history. In this section, we will explain a special character set for English. set - ASCII encoding.

Latin alphabet

Before we formally introduce the ASCII encoding, let's talk about what the Latin alphabet is. It is estimated that many readers, like me, are not very clear about the relationship between the Latin alphabet, the English alphabet and the letters in Chinese Pinyin.

The Latin alphabet, also called the Roman alphabet, is derived from the Greek alphabet and is the most widely used alphabet in the world today. The basic Latin alphabet is the 26 English letters such as ABCD that we often see. The Latin alphabet, the Arabic alphabet, and the Cyrillic alphabet (Cyrillic alphabet) are known as the three major alphabetic systems in the world. The Latin alphabet was originally used by Europeans. Later, due to European colonialism, this alphabet became popular around the world. The Americas, Africa, Australia, and Asia have not escaped the influence of Western culture. The same is true in China. The pinyin we use now is actually the Latin alphabet, which is an out-and-out imported product.

Later, many countries expanded the 26 basic Latin alphabets to suit their local language and culture. The most common way to expand is to add diacritics, such as ü in Chinese Pinyin, which is derived from adding two small dots on the basis of u ; another example, áà is to mark the tones on top of a.

In summary:
  • The basic Latin alphabet is the 26 English letters;
  • The extended Latin alphabet is derived from the basic 26 English letters by adding diacritics, horizontal lines, slashes, etc., and each country is different.

ASCII encoding

Computers were invented by Americans, and the first question they had to consider was how to correspond binary and English letters (that is, Latin).

At that time, various manufacturers or companies had their own practices, and the coding rules were not uniform, which brought a lot of trouble to the data exchange between different computers. But relatively speaking, there are EBCDIC invented by IBM and ASCII which can be generally recognized.

Let's start with ASCII. ASCII is short for "American Standard Code for Information Interchange".
The standard version of ASCII was first released in 1967, and the last update was in 1986. So far, a total of 128 characters have been included, including the basic Latin alphabet (English alphabet), Arabic numerals (that is, 1234567890), punctuation symbols ( ,.! , etc.), special symbols ( @#$%^& , etc.), and some characters with control functions (often not displayed).

In ASCII encoding, uppercase letters, lowercase letters and Arabic numerals are all continuously distributed (see the table below), which brings great convenience to programming. For example, to judge whether a character is an uppercase letter, you can judge whether the ASCII code value of the character is in the range of 65~90.

EBCDIC encoding is just the opposite. Its English letters are not arranged continuously, and there are many interruptions in the middle, which brings some difficulties to programming. Now even IBM itself doesn't use EBCDIC, in favor of the better ASCII.

ASCII encoding has become a universal standard for computers, no one uses EBCDIC encoding anymore, it has disappeared in the long river of history.

ASCII code list

The standard ASCII code contains a total of 128 characters, including 33 control characters (characters that have some special functions but cannot be displayed) and 95 displayable characters.

List of ASCII codes (control characters on a light yellow background and displayable characters on a white background)
binary decimal hex character/abbreviation explain
00000000 0 00 NUL (NULL) null character
00000001 1 01 SOH (Start Of Headling) title starts
00000010 2 02 STX (Start Of Text) text begins
00000011 3 03 ETX (End Of Text) end of text
00000100 4 04 EOT (End Of Transmission) end of transmission
00000101 5 05 ENQ (Enquiry) ask
00000110 6 06 ACK (Acknowledge) respond/respond/receive notification
00000111 7 07 BEL (Bell) bell
00001000 8 08 BS (Backspace) backspace
00001001 9 09 HT (Horizontal Tab) horizontal tab
00001010 10 0A LF/NL(Line Feed/New Line) newline key
00001011 11 0B VT (Vertical Tab) vertical tab
00001100 12 0C FF/NP (Form Feed/New Page) form key
00001101 13 0D CR (Carriage Return) enter
00001110 14 0E SO (Shift Out) no need to switch
00001111 15 0F SI (Shift In) Enable toggle
00010000 16 10 DLE (Data Link Escape) data link escape
00010001 17 11 DC1/XON
(Device Control 1/Transmission On)
Device Control 1/Transfer Start
00010010 18 12 DC2 (Device Control 2) Device Control 2
00010011 19 13 DC3/XOFF
(Device Control 3/Transmission Off)
Device Control 3/Transfer Interruption
00010100 20 14 DC4 (Device Control 4) Device Control 4
00010101 twenty one 15 NAK (Negative Acknowledge) No response/abnormal response/rejection
00010110 twenty two 16 SYN (Synchronous Idle) Sync idle
00010111 twenty three 17 ETB (End of Transmission Block) Transfer Block End/Block Transfer Terminated
00011000 twenty four 18 CAN (Cancel) Cancel
00011001 25 19 EM (End of Medium) End of media reached/Media storage full/Media interrupted
00011010 26 1A SUB (Substitute) Substitute/replace
00011011 27 1B ESC (Escape) escape/cancel
00011100 28 1C FS (File Separator) file separator
00011101 29 1D GS (Group Separator) group separator/grouping character
00011110 30 1E RS (Record Separator) record separator
00011111 31 1F US (Unit Separator) unit separator
00100000 32 20 (Space) space
00100001 33 twenty one !  
00100010 34 twenty two "  
00100011 35 twenty three #  
00100100 36 twenty four $  
00100101 37 25 %  
00100110 38 26 &  
00100111 39 27 '  
00101000 40 28 (  
00101001 41 29 )  
00101010 42 2A *  
00101011 43 2B +  
00101100 44 2C ,  
00101101 45 2D -  
00101110 46 2E .  
00101111 47 2F /  
00110000 48 30 0  
00110001 49 31 1  
00110010 50 32 2  
00110011 51 33 3  
00110100 52 34 4  
00110101 53 35 5  
00110110 54 36 6  
00110111 55 37 7  
00111000 56 38 8  
00111001 57 39 9  
00111010 58 3A :  
00111011 59 3B ;  
00111100 60 3C <  
00111101 61 3D =  
00111110 62 3E >  
00111111 63 3F ?  
01000000 64 40 @  
01000001 65 41 A  
01000010 66 42 B  
01000011 67 43 C  
01000100 68 44 D  
01000101 69 45 E  
01000110 70 46 F  
01000111 71 47 G  
01001000 72 48 H  
01001001 73 49 I  
01001010 74 4A J  
01001011 75 4B K  
01001100 76 4C L  
01001101 77 4D M  
01001110 78 4E N  
01001111 79 4F O  
01010000 80 50 P  
01010001 81 51 Q  
01010010 82 52 R  
01010011 83 53 S  
01010100 84 54 T  
01010101 85 55 U  
01010110 86 56 V  
01010111 87 57 W  
01011000 88 58 X  
01011001 89 59 Y  
01011010 90 5A Z  
01011011 91 5B [  
01011100 92 5C \  
01011101 93 5D ]  
01011110 94 5E ^  
01011111 95 5F _  
01100000 96 60 `  
01100001 97 61 a  
01100010 98 62 b  
01100011 99 63 c  
01100100 100 64 d  
01100101 101 65 e  
01100110 102 66 f  
01100111 103 67 g  
01101000 104 68 h  
01101001 105 69 i  
01101010 106 6A j  
01101011 107 6B k  
01101100 108 6C l  
01101101 109 6D m  
01101110 110 6E n  
01101111 111 6F o  
01110000 112 70 p  
01110001 113 71 q  
01110010 114 72 r  
01110011 115 73 s  
01110100 116 74 t  
01110101 117 75 u  
01110110 118 76 v  
01110111 119 77 w  
01111000 120 78 x  
01111001 121 79 y  
01111010 122 7A z  
01111011 123 7B {  
01111100 124 7C |  
01111101 125 7D }  
01111110 126 7E ~  
01111111 127 7F DEL (Delete) delete

The above table lists the standard ASCII encoding, which contains a total of 128 characters, and the lower 7 bits (Bit) in a byte are enough to represent (2 7 = 128), so the next bit will be free bit, it is wasted.
  1. C program for ASCII encoding and decoding:

    #include <stdio.h>
    
    int main() {
        char character = 'A';
        int asciiValue = (int)character;
    
        printf("ASCII value of %c is %d\n", character, asciiValue);
    
        return 0;
    }
    
    • Demonstrates how to obtain the ASCII value of a character.
  2. Using ASCII characters in C strings:

    #include <stdio.h>
    
    int main() {
        char asciiString[] = "Hello";
        printf("ASCII representation: %s\n", asciiString);
    
        return 0;
    }
    
    • Uses ASCII characters in a C string.