Java转义字符Escape Sequences
Escape Sequence | Description |
---|---|
\t | Insert a tab in the text at this point. |
\b | Insert a backspace in the text at this point. |
\n | Insert a newline in the text at this point. |
\r | Insert a carriage return in the text at this point. |
\f | Insert a formfeed in the text at this point. |
\’ | Insert a single quote character in the text at this point. |
\” | Insert a double quote character in the text at this point. |
\ | Insert a backslash character in the text at this point. |
代码中的表示
final char backspace = '\b';
final char tab = '\t';
final char lineFeed = '\n';
final char carriageReturn = '\r';
final char doubleQuote = '\"';
final char singleQuote = '\'';
final char backslash = '\\';
ASCII码,Unicode,UTF-8的关系
ASCII
码是美国于上个世纪60年代制定的一套字符编码方式,ASCII 码一共规定了128个字符的编码,这128个符号(包括32个不能打印出来的控制符号),只占用了一个字节的后面7位,最前面的一位统一规定为0。
Unicode
将世界上所有的符号都纳入其中。这是一种所有符号的编码。Unicode
只是一个符号集,它只规定了符号的二进制代码,却没有规定这个二进制代码应该如何存储。
UTF-8 是 Unicode 的实现方式之一
UTF-8
就是在互联网上使用最广的一种 Unicode 的实现方式。其他实现方式还包括 UTF-16
(字符用两个字节或四个字节表示)和 UTF-32
(字符用四个字节表示)。
UTF-8
使用1~4个字节表示一个符号,是一种变长的编码方式。
编码规则
1)对于单字节的符号,字节的第一位设为0
,后面7位为这个符号的 Unicode
码。因此对于英语字母,UTF-8
编码和 ASCII
码是相同的。
2)对于n
字节的符号(n > 1
),第一个字节的前n
位都设为1
,第n + 1
位设为0
,后面字节的前两位一律设为10
。剩下的没有提及的二进制位,全部为这个符号的Unicode
码。字母x
表示可用编码的位。
Unicode符号范围(十六进制) | (二进制)UTF-8编码方式 |
---|---|
0000 0000-0000 007F | 0xxxxxxx |
0000 0080-0000 07FF | 110xxxxx 10xxxxxx |
0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx |
0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
编码储存顺序
Unicode
规范定义,每一个文件的最前面分别加入一个表示编码顺序的字符。第一个字节在前,就是”大头方式
“(Big endian),第二个字节在前就是”小头方式
“(Little endian)。
拓展
UTF-8, a transformation format of ISO 10646
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)