What is the difference between ASCII Chr(10) and Chr(13)

May 17, 2019
misc

typewriters have a carriage return

Writing this because I can never remember which ascii code is \n and which is \r. Usually I end up googling my ASCII Cheatsheet for the answer. So here it is fully explained in case you are curious:

What is Chr(10)

The ASCII character code 10 is sometimes written as \n and it is sometimes called a New Line or NL. ASCII character 10 is also called a Line Feed or LF.

On a UNIX based operating system such as Linux or Mac it is all you typically use to delineate a line in a file.

What is Chr(13)

The ASCII character code 13 is called a Carriage Return or CR. On windows based computers files are typically delimited with a Carriage Return Line Feed or CRLF. So that is a Chr(13) followed by a Chr(10) that compose a proper CRLF.

Things such as HTTP headers or MIME mail headers are also delimited with a CRLF.

If you have ever opened a file with notepad on windows and found that it appears that everything is on one line, but then open it in another editor to find that the lines appear, the reason for that is that the file is delimited with only LF Chr(10) and not CRLF. Notepad is not smart enough to know the difference and plops everything on one line. Most text editors on windows besides notepad have no problem with working on files that are only delimited with \n or Chr(10).

This all dates back to the typewriter

The term Carriage Return actually dates back to when typewriters were commonly used. When you hit enter on a typewriter it will advance the paper to the next line (line feed!), however unless you return the carriage (that lever that you move to the left) you would keep typing, here's an example:

I will hit enter now
                    but I forgot to return the carriage

So what should I use as a file line delimiter?

I tend to use only Chr(10) because it takes less space. Unless you have a use case where people will be opening the files you create with notepad, this should work fine.

Also if your file needs to be interfaced with a typewriter, I would make sure you are using a Carriage Return ;-)

Is it Char(10) or Chr(10)?

The name of the function to return an ascii character from an integer character code is not consistent across programming languages. So it depends what language you are using, here is a handy table with some popular languages:

LanguageLine Feed / New LineCarriage Return
Regular Expressions or RegEx\n\r
SQLCHAR(10)CHAR(13)
CFML / ColdFusionChr(10)Chr(13)
PHPChr(10)Chr(13)
PythonChr(10)Chr(13)
JavaScriptString.fromCharCode(10)String.fromCharCode(13)

Like this? Follow me ↯


2 people found this page useful, what do you think?

Post a Comment




  






Foundeo Inc.