Newline Characters in Hex — LF, CR, CRLF
Understand line ending representations in hexadecimal: LF (0A), CR (0D), and CRLF (0D 0A). Debug line ending issues across Windows, Unix, and classic Mac systems.
Hex
0D 0A
ASCII
\r\n (CRLF)
Detailed Explanation
Line endings are represented differently across operating systems, and this difference is a persistent source of bugs. In hexadecimal, the three standard line ending conventions are: LF (0A), CR (0D), and CRLF (0D 0A). Understanding these hex values is critical for debugging text file issues, parsing network protocols, and ensuring cross-platform compatibility.
The three line ending styles:
| Style | Hex | Escape | Used By |
|---|---|---|---|
| LF (Line Feed) | 0A |
\n |
Unix, Linux, macOS (since OS X), modern standard |
| CR (Carriage Return) | 0D |
\r |
Classic Mac OS (pre-OS X, rarely seen today) |
| CRLF | 0D 0A |
\r\n |
Windows, HTTP, SMTP, FTP, most internet protocols |
Origin of CR and LF:
These characters originate from mechanical teletype machines. Carriage Return (CR, 0D) physically moved the print head back to the beginning of the line. Line Feed (LF, 0A) advanced the paper by one line. Performing both operations (CR followed by LF) started a new line, which is why Windows and network protocols use the two-character sequence.
Spotting line endings in hex dumps:
When you open a text file in a hex editor, line endings are immediately visible:
- Unix file:
...48 65 6C 6C 6F 0A 57 6F 72 6C 64...("Hello\nWorld") - Windows file:
...48 65 6C 6C 6F 0D 0A 57 6F 72 6C 64...("Hello\r\nWorld") - Mixed endings: Both
0Aand0D 0Apatterns appear (a common sign of trouble)
Common problems caused by line endings:
- Git diffs showing entire files changed — often caused by line ending conversion
- Shell scripts failing on Linux — a script edited on Windows has
\r\nendings, and the shell interprets\ras part of the command - CSV parsing errors — some parsers expect one style but receive another
- HTTP protocol violations — HTTP headers must end with
\r\nper RFC specification; using only\nmay cause parsing failures in strict implementations
Network protocols and CRLF:
HTTP, SMTP, FTP, and most internet protocols mandate CRLF (0D 0A) as the line separator. An HTTP response header block ends with a double CRLF (0D 0A 0D 0A), which is the \r\n\r\n sequence that separates headers from the body. When analyzing network captures in hex, look for this four-byte pattern to find the header/body boundary.
Fixing line endings:
In a hex editor, you can fix line ending issues by searching for 0D 0A and replacing with 0A (to convert to Unix style) or vice versa. Many modern text editors and Git (via .gitattributes and core.autocrlf) handle this conversion automatically.
Use Case
Developers use hex-level line ending analysis when debugging cross-platform text file issues, investigating shell script failures after Windows editing, or parsing HTTP traffic in network captures.