What is the difference between ASCII Chr(10) and Chr(13)
By Pete Freitag
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:
Language | Line Feed / New Line | Carriage Return |
---|---|---|
Regular Expressions or RegEx | \n | \r |
SQL | CHAR(10) | CHAR(13) |
CFML / ColdFusion | Chr(10) | Chr(13) |
PHP | Chr(10) | Chr(13) |
Python | Chr(10) | Chr(13) |
JavaScript | String.fromCharCode(10) | String.fromCharCode(13) |
Java | Character.toString(10) | Character.toString(13) |
What is the difference between ASCII Chr(10) and Chr(13) was first published on May 17, 2019.
Discuss / Follow me on Twitter ↯
Tweet Follow @pfreitagComments
I think if you look up how an electro-mechanical Telex machine used to work you will find that you needed both carriage return AND line feed in that order for it to work nicely. It took longer for the print head to go all the way to the left so you initiated that first.
If you wanted to cross out a line, you gave carriage return and a load of XXXXX...
AND Yes, I'm old and ugly enough to have used a Telex machine!! :-)
With Regards From
Roger Jefferyes
On a manual mechanical typewriter, the paper is mounted on a roller called a carriage. Between the paper and the key hammers is an ink ribbon. When typing, the key action moves the carriage to the left after the character is impressed on the paper through the ink ribbon. To return to the writing by hand analogy, it would be like keeping the pen hand still and moving the paper underneath to the left instead.
On a manual mechanical typewriter the carriage has a carriage return lever. The first part of its travel advances the paper on the carriage roller. That is the line feed. Continued pressure on the carriage return lever causes the carriage to slide to the right until the left edge of the paper is under the character key hammers. That is a carriage return.
As printing technology developed, the print head moved left to right and paper stayed in same place just moving up a line. However the terminology remained true to a mechanical typewriter.