我想知道两者之间的区别(如果可能的话,请举例说明) CR LF (Windows)、LF (Unix)和CR (Macintosh)三种换行类型。
它实际上只是关于文件中存储哪些字节。CR是一个字节码,用于回车(从打字机时代开始),LF类似地用于换行。它只是引用作为行末标记的字节。
维基百科上总是有更多的信息。
CR和LF为控制字符,分别编码为0x0D(十进制13)和0x0A(十进制10)。
它们用于标记文本文件中的换行符。正如您所指出的,Windows使用两个字符CR LF序列;Unix只使用LF,旧的MacOS(前osx MacIntosh)使用CR。
杜撰的历史观点:
As indicated by Peter, CR = Carriage Return and LF = Line Feed, two expressions have their roots in the old typewriters / TTY. LF moved the paper up (but kept the horizontal position identical) and CR brought back the "carriage" so that the next character typed would be at the leftmost position on the paper (but on the same line). CR+LF was doing both, i.e. preparing to type a new line. As time went by the physical semantics of the codes were not applicable, and as memory and floppy disk space were at a premium, some OS designers decided to only use one of the characters, they just didn't communicate very well with one another ;-)
大多数现代文本编辑器和面向文本的应用程序提供选项/设置等,允许自动检测文件的行尾约定并相应地显示它。
基于ASCII或a的系统 兼容字符集使用任意LF (换行,0x0A, 10 十进制)或CR(回车,0x0D,十进制13) 单独,或CR后面跟着 LF (CR+LF, 0x0D 0x0A); 这些字符基于打印机命令:换行 表示了一行 纸张应从打印机中送出,并有一个马车返回 显示打印机 回车应返回到当前行的开头。
这是细节。
这是我发现的一个很好的总结:
回车符(CR)字符(0x0D, \r)将光标移动到行首,而不移动到下一行。此字符在Commodore和早期Macintosh操作系统(OS-9及更早版本)中用作新行字符。
换行(LF)字符(0x0A, \n)将光标向下移动到下一行,而不返回到行首。此字符在基于UNIX的系统(Linux、Mac OSX等)中用作新行字符。
行结束(EOL)序列(0x0D 0x0A, \r\n)实际上是两个ASCII字符,是CR和LF字符的组合。它将光标向下移动到下一行和该行的开头。该字符在大多数其他非unix操作系统(包括Microsoft Windows、Symbian OS和其他操作系统)中用作新行字符。
源
CR - ASCII码13
LF - ASCII码10。
理论上CR将光标返回到第一个位置(左侧)。LF输入一行移动光标一行。在过去,你就是这样控制打印机和文本模式显示器的。 这些字符通常用于标记文本文件中的行结束。 不同的操作系统使用不同的约定。正如你指出的,Windows使用CR/LF组合,而在osx之前的mac只使用CR等等。
“记录分隔器”或“行终止器”的可悲状态是计算黑暗时代的遗产。
现在,我们理所当然地认为我们想要表示的任何东西在某种程度上都是结构化的数据,并且符合定义行、文件、协议、消息、标记等的各种抽象。
But once upon a time this wasn't exactly true. Applications built-in control characters and device-specific processing. The brain-dead systems that required both CR and LF simply had no abstraction for record separators or line terminators. The CR was necessary in order to get the teletype or video display to return to column one and the LF (today, NL, same code) was necessary to get it to advance to the next line. I guess the idea of doing something other than dumping the raw data to the device was too complex.
Unix和Mac实际上为行尾指定了一个抽象,想象一下。遗憾的是,他们指定的是不同的。(Unix,咳咳,是先出现的。)当然,他们使用的控制代码已经“接近”于S.O.P.
由于今天我们几乎所有的操作软件都是Unix、Mac或MS操作SW的后代,我们陷入了行尾的混乱。
杰夫·阿特伍德最近有一篇关于这个问题的博客文章:伟大的换行符分裂
以下是维基百科的精华:
The sequence CR+LF was in common use on many early computer systems that had adopted teletype machines, typically an ASR33, as a console device, because this sequence was required to position those printers at the start of a new line. On these systems, text was often routinely composed to be compatible with these printers, since the concept of device drivers hiding such hardware details from the application was not yet well developed; applications had to talk directly to the teletype machine and follow its conventions. The separation of the two functions concealed the fact that the print head could not return from the far right to the beginning of the next line in one-character time. That is why the sequence was always sent with the CR first. In fact, it was often necessary to send extra characters (extraneous CRs or NULs, which are ignored) to give the print head time to move to the left margin. Even after teletypes were replaced by computer terminals with higher baud rates, many operating systems still supported automatic sending of these fill characters, for compatibility with cheaper terminals that required multiple character times to scroll the display.
既然没有这样的答案,我就简单地总结一下:
回车(MAC pre-OSX)
CR r \ ASCII码13
换行(Linux, MAC OSX)
低频 \ n ASCII码10
回车和换行(Windows)
CRLF \ r \ n ASCII码13,ASCII码10
如果你看到奇怪格式的ASCII码,它们只是不同基数/基数的数字13和10,通常以8为基数(八进制)或以16为基数(十六进制)。
http://www.bluesock.org/~willg/dev/ascii.html
NL源自EBCDIC NL = x'15',这将在逻辑上与CRLF x'odoa ascii进行比较…当将数据从大型机物理移动到中端时,这一点变得非常明显。口头上(只有奥术人士才会使用缩略语),NL已经等同于CR或LF或CRLF
CR和LF是一组特殊的字符,帮助我们格式化代码。
CR(/r)代表回车。它将光标放在一行的开头,但不创建新行。这就是MAC OS的工作原理。 LF(/n)表示换行。它创建了一个新行,但没有把光标放在该行的开头。光标停留在最后一行的末尾。这就是Unix和Linux的工作原理。 CRLF (/r/f)创建新行,并将光标放在新行开头。这就是我们在Windows操作系统中看到的情况。
Git默认使用LF。所以当我们在Windows上使用Git时,它会抛出一个警告,比如“CRLF将被LF取代”,并自动将所有CRLF转换为LF,这样代码就变得兼容了。 注意:别担心…不要把这看作是一个警告,而要把它看作是一个提醒。