CRLF
May 20, 2023
CRLF stands for Carriage Return Line Feed. It is a sequence of two control characters that are commonly used to indicate line breaks in text files on Windows operating systems. The CRLF sequence is represented by the hexadecimal code 0D 0A, where 0D stands for the Carriage Return character, and 0A stands for the Line Feed character.
In contrast, Unix-like systems, such as Linux and macOS, use only the Line Feed character to indicate line breaks, while the older Macintosh operating system uses only the Carriage Return character. As a result, files created on these systems may not display properly on Windows systems, and vice versa, if the appropriate line ending conversion is not performed.
Purpose
The purpose of the CRLF sequence is to indicate the end of a line of text in a file. The Carriage Return character (0D) moves the cursor to the beginning of the current line, and the Line Feed character (0A) moves the cursor to the beginning of the next line. The sequence is used in plain text files, such as HTML, CSS, JavaScript, and other programming languages, to create a new line within a block of text.
In addition, the CRLF sequence is used in network protocols, such as HTTP, FTP, and SMTP, to separate lines of text in messages. For example, in the HTTP protocol, each header field in a request or response message is terminated by a CRLF sequence.
Usage
The usage of the CRLF sequence is critical for text files to display properly across multiple operating systems. For example, when a text file is created on a Windows system and transferred to a Unix-like system without converting the line endings, the file may appear as a single long line of text, because the Unix-like system only recognizes the Line Feed character as a line break. Similarly, if a text file is created on a Unix-like system and transferred to a Windows system without converting the line endings, the file may display with extra blank lines, because the Windows system recognizes the Carriage Return character as a line break and adds an additional Line Feed character.
To avoid these issues, text editors, code editors, and other software tools typically provide options to convert line endings between the different formats. For example, the popular code editor Visual Studio Code provides an option to change the line endings of a file from CRLF to LF or vice versa.
Example
Consider the following example of a text file with the CRLF sequence:
Hello, world! 0D 0A
This is a new line. 0D 0A
In this example, the first line contains the text “Hello, world!” followed by the CRLF sequence, indicating the end of the line. The second line contains the text “This is a new line.” followed by another CRLF sequence, indicating the end of the line.
When this file is opened on a Windows system, it will display as:
Hello, world!
This is a new line.
On a Unix-like system, it will display as:
Hello, world! This is a new line.
If this file is transferred to a Unix-like system without converting the line endings, it would display as a single long line of text:
Hello, world! 0D 0A This is a new line. 0D 0A