ASCII and binary files

Source: Internet
Author: User
Tags printable characters ftp client

What we use most in linux is File Operations. As long as we access the operating system, file operations are performed. Last time, I mentioned linux File System and windows File System and seven file types in linux. The classification of files is based on the basic attributes of files. files can be classified into ASCII files (also called text files) and binary files from the perspective of file encoding.

Because the data of the two files is stored in binary format in the computer, the difference between ASCII and binary files is not physical, but logical composition. Different logical components can form different storage contents.

An ASCII code file is a fuzzy concept, but most files are considered to be ASCII files used to store characters. The number of bytes in the file is a certain character encoding form, such as: ASCII code/UTF-8 Code); on a linux terminal, you can use the command cat to view the character content, you can use the vim editor to edit the characters. For example, all the source files edited by vi are ASCII files.

The binary file is not used to save characters. The number of bytes in the file indicates other meanings. Some bytes in the executable file indicate commands, and some bytes indicate the positions of each Section and Segment in the file, some bytes indicate the loading address of each Segment ). For example, the target file, executable file, and library file. Use the hexdump command to view a binary file.

Now I am doing a small experiment to distinguish binary files from text files:

In contrast to the Windows operating system, I now edit a text.txt file in Windows as an ASCII file), and then enter "12345 press ENTER ":

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/00264ST0-0.png "title =" 1.png" alt = "215845684.png"/>

I moved this file to the linux operating system and used the od command to view the file storage format:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/00264SP3-1.png "title =" 2.png" alt = "220222907.png"/>

Then, use the catcommand to display the content of text.txt text:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/00264U605-2.png "title =" 3.png" alt = "220336674.png"/>

Here we can easily understand windows's carriage return processing for text files with ASCII code:

File input \ n, which is converted to \ r \ n. When output, \ r \ n is converted to \ n.


In linux, you can directly use VIM to edit the same file:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/00264SO6-3.png "title =" 4.png" alt = "220924571.png"/>

Then we can use the same command to check its storage method:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/00264Q2T-4.png "title =" 5.png" alt = "221149706.png"/>

In linux, the system automatically adds '\ n' to the ASCII code file, which occupies 7 bytes;

Let's take a look at the content displayed in the file itself:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/00264R955-5.png "title =" 6.png" alt = "221447715.png"/>

The content is displayed according to the content edited under vim.



Next let's take a look at the processing of binary files:

I tried to open a binary file in windows, but it eventually failed! However, it can be predicted that the vast majority of opened files are 0 or 1 sequences or garbled characters. in linux, I opened a binary file:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/00264RS6-6.png "title =" 7.png" alt = "222328147.png"/>

Garbled sequence Display.

Later, I found on the Internet that the windows operating system does not convert binary files; linux does not distinguish between ASCII and binary files.


Since we talk about binary and ASCII files, we should also know their applications. Below I will briefly introduce the application of these two types of files in transmission:

ASCII transmission mode and binary data transmission mode are two FTP transmission modes.

1. ASCII transmission mode: assume that the file being copied contains simple ASCII code text. If the file running on a remote machine is not UNIX, during file transfer, ftp usually automatically adjusts the content of the file so that the file can be interpreted as another computer that stores the text file format.
However, in this case, the files being transmitted by the user are not text files. They may be programs, databases, word processing files, or compressed files. Although most of the word processing files contain text files, it also contains non-printable characters indicating the page size and font information ). Before copying any non-text files, use the binary command to tell ftp to copy them verbatim. Do not process these files. This is also the binary transfer to be discussed below.
2. binary transmission mode: in binary transmission, the file order is saved so that the original and copied files correspond one by one. Even if the destination machine contains files with bit sequences, it makes no sense. For example, if a macintosh transfers an executable file to a Windows system in binary mode, the file cannot be executed on the other system.
If you transfer a binary file in ASCII mode, the translation will still be translated even if you do not need it. This slows down the transmission, damages the data, and makes the file unusable. On most computers, the ASCII mode generally assumes that the first valid bit of each character is meaningless, because it is not used for the combination of ASCII characters. If you transmit binary files, all bits are important .) If you know that the two machines are the same, the binary method is effective for text files and data files.
How FTP works
FTP supports two modes: Standard (PORT mode) and Passive (PASV mode ). The FTP client in Standard mode sends the PORT command to the FTP server. The client in Passive mode sends the PASV command to the FTP Server.
The following describes how these two methods work:
In Port mode, the FTP client first establishes a connection with the tcp port 21 of the FTP server and sends commands through this channel. When the client needs to receive data, it sends the Port command through this channel. The PORT command contains the PORT used by the client to receive data. When transmitting data, the server connects to the specified port of the client through its TCP port 20 to send data. The FTP server must establish a new connection with the client to transmit data.
The Passive mode is similar to the Standard mode when a control channel is established, but the Port command is not sent after the connection is established, but the Pasv command. After the FTP server receives the Pasv command, it randomly opens a high-end port number greater than 1024) and notifies the client to send data requests on this port. The client connects to this port of the FTP server, the FTP server then transmits data through this port. At this time, the FTP server no longer needs to establish a new connection with the client.
Many firewalls do not allow external connections when they are set. Therefore, many FTP servers located behind the firewall or on the intranet do not support the PASV mode, because the client cannot open the high-end PORT of the FTP server through the firewall, many Intranet clients cannot log on to the FTP server in PORT mode, because the slave server's TCP 20 cannot establish a new connection with the client of the internal network, it cannot work.

I believe you have a deep understanding of ASCII and binary files! ^-^


This article from the "road to growth" blog, please be sure to keep this source http://7905648.blog.51cto.com/7895648/1298447

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.