Java基础知识之字符编码

一.字符编码的发展历程:
字符编码的发展历程:

阶段1:
计算机只认识数字,我们在计算机里一切数据都是以数字来表示,因为英文符号有限,
所以规定使用的字节的最高位是0.每一个字节都是以0~127之间的数字来表示,比如A对应65,a对应97.
这就是美国标准信息交换码-ASCII.

阶段2:
随着计算机在全球的普及,很多国家和地区都把自己的字符引入了计算机,比如汉字.
此时发现一个字节能表示数字范围太小,不能包含所有的中文汉字,那么就规定使用两个字节来表示一个汉字.
规定:原有的ASCII字符的编码保持不变,仍然使用一个字节表示,为了区别一个中文字符与两个ASCII码字符,
中文字符的每个字节最高位规定为1(中文的二进制是负数).这个规范就是GB2312编码,
后来在GB2312的基础上增加了更多的中文字符,比如汉字,也就出现了GBK.

阶段3:
新的问题,在中国是认识汉字的,但是如果把汉字传递给其他国家,该国家的码表中没有收录汉字,其实就显示另一个符号或者乱码.
为了解决各个国家因为本地化字符编码带来的影响,咱们就把全世界所有的符号统一进行编码-Unicode编码.
此时某一个字符在全世界任何地方都是固定的,比如’哥’,在任何地方都是以十六进制的54E5来表示.
Unicode的编码字符都占有2个字节大小.

常见的字符集:
ASCII: 占一个字节,只能包含128个符号. 不能表示汉字
ISO-8859-1:(latin-1):占一个字节,收录西欧语言,.不能表示汉字.
ANSI:占两个字节,在简体中文的操作系统中 ANSI 就指的是 GB2312.
GB2312/GBK/GB18030:占两个字节,支持中文.
UTF-8:是一种针对Unicode的可变长度字符编码,称万国码,是Unicode的实现方式之一。
编码中的第一个字节仍与ASCII兼容,这使得原来处理ASCII字符的软件无须或只须做少部份修改,即可继续使用。
因此,它逐渐成为电子邮件、网页及其他存储或传送文字的应用中,优先采用的编码。互联网工程工作小组(IETF)要求所有互联网协议都必须支持UTF-8编码。

UTF-8 BOM:是MS搞出来的编码,默认占3个字节,不要使用这个.

存储字母,数字和汉字:
存储字母和数字无论是什么字符集都占1个字节.
存储汉字: GBK家族占两个字节,UTF-8家族占3个字节.
不能使用单字节的字符集(ASCII/ISO-8859-1)来存储中文.

二.字符的编码和解码操作:
编码: 把字符串转换为byte数组.
解码: 把byte数组转换为字符串.
一定要保证编码和解码的字符相同,否则乱码.
在这里插入图片描述
在这里插入图片描述
三.工具类

  1. import java.io.UnsupportedEncodingException;
  2. /** * 字符编码工具类 */
  3. public class CharTools {
  4. /** * 转换编码 ISO-8859-1到GB2312 * @param text * @return */
  5. public static final String ISO2GB(String text) {
  6. String result = "";
  7. try {
  8. result = new String(text.getBytes("ISO-8859-1"), "GB2312");
  9. }
  10. catch (UnsupportedEncodingException ex) {
  11. result = ex.toString();
  12. }
  13. return result;
  14. }
  15. /** * 转换编码 GB2312到ISO-8859-1 * @param text * @return */
  16. public static final String GB2ISO(String text) {
  17. String result = "";
  18. try {
  19. result = new String(text.getBytes("GB2312"), "ISO-8859-1");
  20. }
  21. catch (UnsupportedEncodingException ex) {
  22. ex.printStackTrace();
  23. }
  24. return result;
  25. }
  26. /** * Utf8URL编码 * @param s * @return */
  27. public static final String Utf8URLencode(String text) {
  28. StringBuffer result = new StringBuffer();
  29. for (int i = 0; i < text.length(); i++) {
  30. char c = text.charAt(i);
  31. if (c >= 0 && c <= 255) {
  32. result.append(c);
  33. }else {
  34. byte[] b = new byte[0];
  35. try {
  36. b = Character.toString(c).getBytes("UTF-8");
  37. }catch (Exception ex) {
  38. }
  39. for (int j = 0; j < b.length; j++) {
  40. int k = b[j];
  41. if (k < 0) k += 256;
  42. result.append("%" + Integer.toHexString(k).toUpperCase());
  43. }
  44. }
  45. }
  46. return result.toString();
  47. }
  48. /** * Utf8URL解码 * @param text * @return */
  49. public static final String Utf8URLdecode(String text) {
  50. String result = "";
  51. int p = 0;
  52. if (text!=null && text.length()>0){
  53. text = text.toLowerCase();
  54. p = text.indexOf("%e");
  55. if (p == -1) return text;
  56. while (p != -1) {
  57. result += text.substring(0, p);
  58. text = text.substring(p, text.length());
  59. if (text == "" || text.length() < 9) return result;
  60. result += CodeToWord(text.substring(0, 9));
  61. text = text.substring(9, text.length());
  62. p = text.indexOf("%e");
  63. }
  64. }
  65. return result + text;
  66. }
  67. /** * utf8URL编码转字符 * @param text * @return */
  68. private static final String CodeToWord(String text) {
  69. String result;
  70. if (Utf8codeCheck(text)) {
  71. byte[] code = new byte[3];
  72. code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256);
  73. code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256);
  74. code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256);
  75. try {
  76. result = new String(code, "UTF-8");
  77. }catch (UnsupportedEncodingException ex) {
  78. result = null;
  79. }
  80. }
  81. else {
  82. result = text;
  83. }
  84. return result;
  85. }
  86. /** * 编码是否有效 * @param text * @return */
  87. private static final boolean Utf8codeCheck(String text){
  88. String sign = "";
  89. if (text.startsWith("%e"))
  90. for (int i = 0, p = 0; p != -1; i++) {
  91. p = text.indexOf("%", p);
  92. if (p != -1)
  93. p++;
  94. sign += p;
  95. }
  96. return sign.equals("147-1");
  97. }
  98. /** * 判断是否Utf8Url编码 * @param text * @return */
  99. public static final boolean isUtf8Url(String text) {
  100. text = text.toLowerCase();
  101. int p = text.indexOf("%");
  102. if (p != -1 && text.length() - p > 9) {
  103. text = text.substring(p, p + 9);
  104. }
  105. return Utf8codeCheck(text);
  106. }
  107. }

发表评论

表情:
评论列表 (有 0 条评论,59人围观)

还没有评论,来说两句吧...

相关阅读

    相关 常见字符编码有关知识

    Unicode:ISO(国际标准化组织)为了解决不同语言之间进行文本转换和处理,为每种语言设置统一的唯一的二进制标准。(简称UCS)。Unicode只是规定如何编码,并没有规定

    相关 编码基础知识

    最早的编码是iso8859-1,和ascii编码相似。但为了方便表示各种各样的语言,逐渐出现了很多标准编码,重要的有如下几个。  1.1. iso8859-1 通常叫做La

    相关 基础学习 Python 字符编码

    编码   要讲字符编码,首先我们要从编码开始谈起。编码这个东西如果单说具体的定义的话可能很难理解,下面我用一个例子解释一下。   大家应该都看过战争片,一般要强攻的时候

    相关 基础知识字符编码

    一、字符编码 1、什么实字符编码:将人识别的字符转换成计算机能识别的01,而转换的过程或者规则就是字符编码表。 而这种字符编码表表示了一种对应关系。 2、常用的字符编