In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the differences between text files and binary files, the article is very detailed, has a certain reference value, interested friends must read it!
Differences between text files and binary files: 1. Text files are based on character encoding, the common encodings are ASCII encoding, UNICODE encoding, etc.; 2. Binary files are based on value encoding.
The difference between text files and binary files:
I. definition of text files and binary files
Everyone knows that computer storage is physically binary, so the difference between text files and binary files is not physical, but logical. The two are only different at the coding level.
To put it simply, text files are based on character encoding, and the common encodings are ASCII encoding, UNICODE encoding and so on. Binaries are files encoded based on values, and you can specify what a value means according to your application (a process that can be thought of as custom coding).
From the above, we can see that the text file is basically a fixed-length encoding, based on characters, each character is fixed in the specific encoding, ASCII code is 8 bits of coding, UNICODE generally accounts for 16 bits. The binary file can be regarded as variable length coding, because it is a value code, and it is entirely up to you to decide how many bits represent a value. You may be familiar with the BMP file, take it as an example, its header is a relatively fixed length of file header information, the first 2 bytes are used to record the file in BMP format, the next 8 bytes are used to record the length of the file, and the next 4 bytes are used to record the length of the bmp header. As you can see, the encoding is value-based (variable length, 2, 4, and 8-byte long values are available), so BMP is a binary file.
II. Access to text files and binary files
What is the process of opening a file by a text tool? Notepad, for example, first reads the file's physically corresponding binary bitstream (as mentioned earlier, the storage is binary), then interprets the stream in the way you choose to decode it, and then displays the result. Generally speaking, the decoding method you choose will be in the form of ASCII code (one character of ASCII code is 8 bits), and then it will interpret the file stream with 8 bits and 8 bits. For example, for such a file stream "0100000001000001" 010000101000011" (underscore'_', which I added manually in order to enhance readability), the first 8-bit '01000000000' decoded by ASCII code, the corresponding character is the character 'BCD', that is, the file stream can be interpreted as "ABCD", and then notepad will display the "ABCD" on the screen.
In fact, if anything in the world wants to communicate with other things, there is an established protocol, an established code. People communicate with each other through words, and the Chinese character "mother" represents the person who gave birth to you. This is an established code. But notice such a situation, the Chinese character "mother" in Japanese may be the person you gave birth to, so when a Chinese An and Japanese B use the word "mother" to communicate, it is normal to have misunderstandings. Opening binaries with notepad is similar to the situation above. Notepad no matter what file is opened according to the established character encoding (such as ASCII code), so when he opens the binary file, garbled is also a very inevitable thing, decoding and decoding do not correspond. For example, the file stream '0000000000000000000000000000000000000000000000000001' may correspond to a four-byte integer int1 in the binary file, which is interpreted as the four control characters "NULL_NULL_NULL_SOH" in notepad.
The storage and reading of text files is basically an inverse process and will not be repeated. The access of binary files is obviously similar to that of text files, but the encoding / decoding methods are different and are no longer described.
Third, the advantages and disadvantages of text files and binary files
Because the difference between text files and binary files is only different in coding, so their advantages and disadvantages are the advantages and disadvantages of coding. It is more clear to find a coded book. It is generally believed that text file coding is based on fixed character length, and decoding is easier; binary file coding is longer, so it is flexible, storage utilization is higher, and decoding is more difficult (different binary file formats have different decoding methods). With regard to space utilization, think about it, a binary file can even use a bit to represent a meaning (bit operation), while any meaning of a text file is at least one character.
Many books also believe that the readability of text files is better, and storage takes conversion time (encoding and decoding for reading and writing), while binary files are poor readability, and there is no conversion time for storage (read and write do not encode and decode, directly write values). The readability here is from the point of view of software users, because we can browse almost all text files with a general notepad tool, so text files are well readable. Reading and writing a specific binary file requires a specific file decoder, so the readability of the binary file is poor, such as reading BMP files, you must use picture-reading software. the storage conversion time here should be from a programming point of view, because some operating systems such as windows need to convert the carriage return newline character (change'\ n'to'\ r\ n', so when the file is read and written The operating system needs an one-character check to see if the current character is'\ n' or'\ r\ n'). This storage conversion is not required in the Linux operating system, which is, of course, possible when sharing files on two different operating systems (such as Linux and Windows systems sharing text files). About how this conversion works, I will give ^ _ ^ in the next article, "conversion between Linux text files and Windows text files".
IV. C text reading and writing and binary reading and writing
It should be said that C text reading and writing and binary reading and writing is a problem at the programming level, which is related to the specific operating system, so "the file read and written in text must be a text file." the view that files read and written in binary must be binary files is wrong. the following description does not explicitly indicate the type of operating system. Both imply that the difference between windows.C text-side reading and writing and binary reading and writing is only reflected in the processing of carriage return newline characters. When writing text, every time a'\ n' (0AH newline character) is encountered, it will replace it with'\ r\ n' (0D0AH, enter newline), and then write to the file. When the text is read, every time it encounters a'\ r\ n', it inversely changes it to'\ nbuffer 'and then sends it to the read buffer. Precisely because there is a conversion between'\ ncopyright'\ r\ n' in the text mode, the conversion is time-consuming. In binary read and write, there is no conversion, and the data in the write buffer is written directly to the file.
The above is all the contents of the article "what's the difference between text files and binary files". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.