Line termination: line feed versus carriage return 0d 0a

Line termination: line feed versus carriage return 0d 0a



Line feed and carriage return are two different ways of computer interpretting the ENTER key. Line feed and carriage return are two different ways how lines are ended in the computer language. Both the line feed and carriage return originate in the typewriter age.

The best way to explain this is is to ask:

"What do you suppose your computer sees when you press the Enter key?"

You can rest assured that your computer translates the ENTER key stroke into something. But while for example the character A is interpreted the same way by all common platforms (Windows, UNIX, Mac), the ENTER key is interpreted differently.

This causes problems when porting files from one platform to another one.

How ENTER works in computer language?

When you press A on your keyboard, you will see letter A on the screen. When you press B on your keyboard, you will see letter B on the screen.

Each of these two characters gets translated into a code that the computer can understand. Each of these two characters is one byte long, and your computer has some algorithm for translating these human readable letters into code numbers.

The computer number assigned to the letter A happens to be 65 (you can find this in the ASCII table). In other words, the decimal representation of the letter or character A is 65. If you translate this into the computer zeros and ones, you will get 1000001, which is 8 bits, that is one byte.

Decimal binary hex or hexadecimal - What is it?

Characters' code values are represented by various utilities using different numbering schemes. Some times we talk about the decimal number system. In this system, letter A is represented by number 65. Letter B is represented by number 66.

Some times (rarely) symbols are expressed in binary number system. In this numbering scheme, letter A would be 1000001 as we have already mentioned, and the letter B would be expressed as 1000010.

Quite often, symbols are expressed using the hexadecimal number system. Letter A in this system is expressed as 41 and letter B is 42.

Decimal 10000001, binary 65, and hexidecimal 41 are different numeric "languages" for the same letter.

There are other numbering schemes such as octal, but we won't get into those.

So, what does my computer see when I hit ENTER?

When you look inside some file that you created, you can find out. All that you need for this job is some hexadecimal editor. You can use for example the PSPad editor.

Open a text file in the text editor and type the following sequence:

A
hit enter
B
hit enter

Now when you open the file in a hexadecimal editor in a HEX view, you should see the following:

 Carriage return 0d line feed 0a

Note that the letter A shows as 41 and the letter B shows as 42.

If your file was created in Linux, you would also see a single character 0a after each letter, that is, wherever you pressed the ENTER key.

If your file was created in Windows, the pair of characters 0d and 0a would appear any place where you pressed the ENTER key.

The hexadecimal 0a, a control character as opposed to a printing character, is called a line feed.

The hexadecimal 0d is called a carriage return.

Pretty much all the programs on the Windows platform understand and expect the hexadecimal 0a0d pair in text. The 0d0a pair of characters is the signal for the end of a line and beginning of another.

On the other hand, a UNIX program expects the single 0a character to denote the same thing.

A problem arises with cross-platform exchange of files. If you transfer files from Linux to Windows or Mac, the software sees something other than what it expects.

Windows versus UNIX versus Macintosh

As if it was not enough, Macintosh makes the whole story even more complicated. The original Mac operating system used carriage return 0d as the line separator. 

So, to summarize, Windows uses carriage return - line feed, UNIX and newer Mac use line feed, and older Macs use carriage return.

WINDOWS:

preceding text -> 0d 0a -> succeeding text

UNIX, Mac OSX:

preceding text -> 0a -> succeeding text

MAC (OS-9 and earlier):

preceding text -> 0d -> succeeding text

How do I fix problems associated with this?

When transferring files from one platform to another, you can experience that ENTERs are not translated in a way thay you would expect. See the next page for more details: Carriage return and line feed problems.

I have troubles and questions

In case you need some assistance with this, feel free to ask in our discussion forum.

.

Discuss this article or this topic in our discussion forum:
(The table bellow shows a list of 8 most recent topics posted in our discussion forum. Visit our discussion forum to see more. It is possible the links below are not related to this page, but you can be certain you will find related posts in the discussion forum. You can post one yourself too.)
Email this article to a friend:
TO: 
FROM: 
2 + 6 - 3 = 
.
How can I link to this web page?

It is easy, just include the code provided below into your HTML code.

<a href="http://www.maxi-pedia.com/line+termination+line+feed+versus+carriage+return+0d0a" title="www.Maxi-Pedia.com: Line termination: line feed versus carriage return 0d 0a" target="_blank">Line termination: line feed versus carriage return 0d 0a</a>
.