LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 7789|回复: 10

求教文本中换行符的ascii码是多少

[复制链接]
发表于 2005-11-24 21:05:41 | 显示全部楼层 |阅读模式
教文本中换行符的ascii码是多少?我编了个程序,要判断文本中的行数,我想知道换行符的ascii码是多少?是10(LF)吗?
发表于 2005-11-24 21:51:12 | 显示全部楼层
'\n'就行,不用知道ascii码吧。
回复 支持 反对

使用道具 举报

发表于 2005-11-25 10:15:57 | 显示全部楼层
10   换行
13   回车
回复 支持 反对

使用道具 举报

发表于 2005-11-25 13:29:23 | 显示全部楼层
int value = (int)'\n';
printf("%d",value);
回复 支持 反对

使用道具 举报

发表于 2005-11-26 21:54:48 | 显示全部楼层
呵呵!~获取这个的方法有很多种,我这里有一种
[gnap@osiris tmp]$ cat > aaa.txt

[gnap@osiris tmp]$ cat aaa.txt

[gnap@osiris tmp]$ hexdump -C aaa.txt
00000000  0a                                                |.|
00000001
[gnap@osiris tmp]$

于是,显然回车是0x0a也就是ASCII码的13。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-11-27 20:30:47 | 显示全部楼层
换行和回车有什么区别
回复 支持 反对

使用道具 举报

发表于 2005-11-27 21:17:32 | 显示全部楼层
0x0a(十进制10)是换行,0x0d(十进制13)是回到行首。
这牵扯到Unix格式和DOS格式的一个不同:
在DOS中,只使用0x0a的话,光标只是到下一行相同的列,而不会回到行首。因此,在DOS下,回车包含两个字符:0x0d 0x0a
在Unix中,0x0a一个字符就完成了到下一行行首的整个动作,而不需要0x0d。

因此DOS和Unix之间需要文本转换才能在另一个平台下正确显示回车。

Post by gnap
于是,显然回车是0x0a也就是ASCII码的13。

0x0a=13!!??
那0x0d等于多少??
回复 支持 反对

使用道具 举报

发表于 2005-11-29 23:47:19 | 显示全部楼层
man ascii

有man都不用,太浪费了
回复 支持 反对

使用道具 举报

发表于 2005-11-30 00:50:41 | 显示全部楼层
From Practical C Progarmming: The End-of-Line Puzzle
Back in the dark ages BC (Before Computers), a magical device called a Teletype
Model 33 existed. This amazing machine contained a shift register made out of a
motor, with a rotor, and a keyboard ROM consisting solely of levers and springs. It
contained a keyboard, a printer, and a paper tape reader/punch. It could transmit
messages over the phones using a modem at the rate of 10 characters a second.
The Teletype had a problem. It took two-tenths of a second to move the printhead
from the right side to the left. Two-tenths of a second is two character times. If a
second character came while the printhead was in the middle of a return, that
character was lost.
The Teletype people solved this problem by making end-of-line two characters:
<RETURN> to position the printhead at the left margin and <LINE FEED> to move
the paper up one line.
When the early computers came out, some designers realized that using two
characters for end-of-line wasted storage (at this time, storage was very expensive).
Some picked <LINE FEED> for their end-of-line, some <RETURN>. Some of the
diehards stayed with the two -character sequence.
UNIX uses <LINE FEED> for end-of-line. The newline character, \n, is code 0x0A
(LF or <LINE FEED>). MS-DOS/Windows uses the two characters: <LINE
FEED><RETURN>. Apple uses <RETURN>.
回复 支持 反对

使用道具 举报

发表于 2005-12-1 09:44:14 | 显示全部楼层
谢谢楼上提供的背景资料。
以前知道“UNIX uses <LINE FEED> for end-of-line. The newline character, \n, is code 0x0A
(LF or <LINE FEED>). MS-DOS/Windows uses the two characters: <LINE
FEED><RETURN>. ”,而“Apple uses <RETURN>” 呵呵,读了才知道的。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表