DATA REPRESENTATION CHECKS

Checking byte changes after newline normalization

Two files can show the same line break on screen and still differ in their bytes. This guide counts the bytes behind LF and CRLF and shows what a normalization step changes.

One line break, two byte conventions

A line break is stored as one or two control characters. Unix, Linux and modern macOS text files end a line with a line feed, called LF, which is character code 10 and byte 0A in hexadecimal. Windows text files traditionally end a line with a carriage return followed by a line feed, called CRLF, which is codes 13 and 10 and bytes 0D 0A. An editor draws both as the start of a new line, so the difference is invisible while reading. It is visible in a hex dump, in a file size, in a checksum and in any program that compares or splits text by exact characters.

The LF version, three bytes

Take the text A, line feed, B. The UTF-8 byte converter returns the hexadecimal bytes 41 0A 42. The letter A is 41 and the letter B is 42, and the line break between them is the single byte 0A. All three characters are in the ASCII range, so each one takes exactly one byte in UTF-8, and the total length is 3. Characters outside ASCII take more: in UTF-8, an accented letter such as é takes two bytes and many symbols take three or four. That is why a byte count and a character count can differ for general text, while for a line break itself the count is always 1 for LF and 2 for CRLF. The converter reports the characters you supply in a deterministic way, and for plain ASCII text the number of characters and the number of bytes are the same.

The CRLF version, four bytes

Now take A, carriage return, line feed, B. The converter returns 41 0D 0A 42. The two newline characters stay separate: 0D is the carriage return and 0A is the line feed that follows it. The byte length calculator confirms a length of 4, because it counts both separator characters. The same visible text has gained one byte for each line break. Over a file with ten thousand lines, that is ten thousand extra bytes, and every one of them is a character that a strict comparison will notice.

Scaling up to a longer text

Three short lines, A, B and C, separated by two line breaks, take 5 bytes with LF and 7 bytes with CRLF. The difference equals the number of line breaks, which is 2 here. If the last line also ends with a break, the count grows by one more for each style. A lone carriage return, which some old systems used, would add one byte per break like LF but with code 0D. The arithmetic is simple: the byte total is the visible character count plus one or two per line break, depending on the convention in use. A log with 1,000 lines written with CRLF is therefore 1,000 bytes larger than the same log written with LF. For small files that rarely matters, but for transfer limits, fixed-size fields or exact size checks it can decide whether a file passes.

Where the difference causes trouble

A script written for LF may read a CRLF file and keep the carriage return at the end of every line, so a value that should be the word done becomes done followed by an invisible 0D. Comparisons then fail, and the failure is hard to spot because the printed text looks right. Version control systems can show an entire file as changed when only the line endings differ. Shell scripts with CRLF endings can fail on some systems with an error about an unknown interpreter, since the carriage return becomes part of the first line. Hash values also change, because they depend on every byte. The names come from the typewriter era: carriage return moved the print head back to the left edge and line feed advanced the paper by one line, so teleprinters needed both actions to start a new line.

Normalizing changes the content

Converting CRLF to LF, or the reverse, is a real edit of the bytes, not a cosmetic one. After the conversion the file has a different size and a different checksum, and any signature or hash recorded earlier no longer matches. Text-mode transfers and some editors convert endings automatically, which is why a file can change between machines without anyone typing a character. When a workflow needs byte-for-byte identity, such as signed files or reproducible builds, the line-ending style has to be fixed and kept the same on every step.

Choose and document a convention

Many projects settle the question with a written rule, and tools such as Git offer settings that normalize endings at commit time or at checkout. An editor configuration file can set the ending style per project. Documenting the choice avoids silent conversions in the middle of a pipeline. Some formats fix the convention themselves: the CSV standard in RFC 4180 specifies CRLF between records, while HTTP header lines also end with CRLF. A tool consuming such data should accept what the format allows and not assume the convention of the editor that made the file.

What the byte tools report

The UTF-8 byte tools describe the characters you supply. They do not choose a newline convention, and they cannot guarantee that a later editor or upload form preserves the one you typed. Pasting into a web form can turn CRLF into LF, or the reverse, before the tool even sees the text. So treat the reported bytes as a statement about this exact input. For a file on disk, a hex viewer shows the stored bytes, and the byte length calculator can confirm a pasted sample against what you expect.

Reading the numbers back

To find out which convention a text uses, encode a short sample around a line break. Seeing 0A alone means LF. Seeing 0D 0A means CRLF, and a lone 0D means the old carriage-return style. Compare the byte length with the character count of the visible text and add the breaks. Any mismatch points to hidden characters. Keep any normalization step explicit in a data workflow, and record which ending style the downstream system expects, so a later change in tooling does not alter the content unnoticed. A quick acceptance test is to encode a three-character sample such as A, break, B before and after each stage and confirm the hex output still reads 41 0A 42, or 41 0D 0A 42, as the convention requires.

Reproduce the checks

Line-feed bytes

Open the Utf8 Byte Converter and enter:

Text or encoded input
A
B
Calculation mode
encode

Expected output:

Hex

41 0A 42

The line break uses one ASCII byte.

Two-character newline bytes

Open the Utf8 Byte Converter and enter:

Text or encoded input
A
B
Calculation mode
encode

Expected output:

Hex

41 0D 0A 42

The two newline characters remain separate.

Two-character-newline length

Open the Utf8 Byte Length Calculator and enter:

Text or encoded input
A
B

Expected output:

Bytes

4

Byte length includes both separator characters.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks