A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wata 中级黑马   /  2015-1-13 16:49  /  849 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package day21;

  2. import java.io.*;
  3. import java.util.*;

  4. /*
  5. * 编码:字符串(String)-->字节数组(byte[])
  6. * str.getbytes(charsetName)
  7. *
  8. * 解码:字节数组(byte[])-->字符串(String)
  9. * new String(byte[], charsetName)
  10. *
  11. * -------------------------------------
  12. *
  13. * 默认编码和解码都是GBK,
  14. * GBK编码:两个字节表示一个字符
  15. * UTF-8编码:三个字节表示一个字符
  16. *
  17. */

  18. public class EncodeDemo {

  19.         public static void main(String[] args) throws Exception {
  20.                 //bianma();
  21.                 //jiema();
  22.                 bianma_jiema();
  23.         }
  24.        
  25.         //对错误解码进行重新编码和解码
  26.         public static void bianma_jiema() throws Exception{
  27.                 String str = "你好";
  28.                
  29.                 //对str进行GBK编码
  30.                 byte[] b = str.getBytes("GBK");
  31.                 System.out.println(Arrays.toString(b));
  32.                
  33.                 //对b进行错误的ISO8859-1解码
  34.                 String str2 = new String(b,"ISO8859-1");
  35.                 System.out.println(str2);
  36.                
  37.                 //对str2进行ISO8859-1编码
  38.                 byte[] b2 = str2.getBytes("ISO8859-1");
  39.                 System.out.println(Arrays.toString(b2));
  40.                
  41.                 //对b2进行GBK解码
  42.                 String str3 = new String(b2, "GBK");
  43.                 System.out.println(str3);
  44.                
  45.                 /*
  46.                          结果:
  47.                         
  48.                         [-60, -29, -70, -61]
  49.                         ????
  50.                         [-60, -29, -70, -61]
  51.                         你好
  52.                  */
  53.         }
  54.        
  55.         //解码演示
  56.         public static void jiema() throws Exception{
  57.                 String s = "你好";
  58.                
  59.                 //对s进行编码
  60.                 byte[] b1 = s.getBytes("GBK");
  61.                 byte[] b2 = s.getBytes("UTF-8");
  62.                
  63.                 //解码
  64.                 String s1 = new String(b1);//默认解码是GBK
  65.                 String s2 = new String(b1,"GBK");//GBK-->GBK
  66.                 String s3 = new String(b1,"UTF-8");//GBK-->UTF-8
  67.                 String s4 = new String(b2);//默认解码是GBK
  68.                 String s5 = new String(b2,"GBK");//UTF-8-->GBK
  69.                 String s6 = new String(b2,"UTF-8");//UTF-8-->UTF-8
  70.                
  71.                 //打印解码内容
  72.                 System.out.println("s1 = "+s1);//你好
  73.                 System.out.println("s2 = "+s2);//你好
  74.                 System.out.println("s3 = "+s3);//???
  75.                 System.out.println("s4 = "+s4);//浣犲ソ
  76.                 System.out.println("s5 = "+s5);//浣犲ソ
  77.                 System.out.println("s6 = "+s6);//你好
  78.         }
  79.        
  80.         //编码演示
  81.         public static void bianma()throws Exception{
  82.                 String s = "你好";
  83.                
  84.                 //编码
  85.                 byte[] b1 = s.getBytes();//默认编码是GBK
  86.                 byte[] b2 = s.getBytes("GBK");//指定GBK编码,GBK编码是两个字节代表一个字符
  87.                 byte[] b3 = s.getBytes("UTF-8");//指定UTF-8编码,UTF-8编码是三个字节代表一个字符
  88.                
  89.                 //打印编码结果
  90.                 System.out.println(Arrays.toString(b1));//[-60, -29, -70, -61]
  91.                 System.out.println(Arrays.toString(b2));//[-60, -29, -70, -61]
  92.                 System.out.println(Arrays.toString(b3));//[-28, -67, -96, -27, -91, -67]
  93.         }

  94. }
复制代码


评分

参与人数 1技术分 +2 收起 理由
lwj123 + 2 很给力!

查看全部评分

1 个回复

倒序浏览
补充:

  1. package day21;

  2. /*

  3. 字符“联通”的字节二进制编码是:
  4. 11000001
  5. 10101010
  6. 11001101
  7. 10101000
  8. 刚好符合UTF-8的编码机制,
  9. 所以解码的时候用的是UTF-8解码,所以以出现乱码

  10. ---------------------------

  11. UTF-8的编码机制在API文档的java.io包DataInput接口中有

  12. */

  13. public class EncodeDemo2 {
  14.         public static void main(String[] args)throws Exception{
  15.                
  16.                 String str = "联通";
  17.                 byte[] b = str.getBytes("GBK");
  18.                 for(byte by : b){
  19.                         System.out.println(Integer.toBinaryString(by&255));
  20.                         //by的二进制形式是32位,而我们需要的只是后8位,所以用by&255操作获取后8位
  21.                 }
  22.         }
  23. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马