一。编码问题

  • utf-8编码中,一个中文占3个字节,一个英文占1个字节;gbk编码中,一个中文占2个字节,一个英文占1个字节。
  • Java是双字节编码,为utf-16be编码,是说一个字符(无论中文还是英文,都占用2个字节)。因此如果这么问:Java字符串中一个字符可以放一个中文吗?是可以的!
  • 如果一直某个字节序列的编码方式,当我们想将它还原成字符串时,应明确指定其编码格式,否则会出现乱码。
  • 文本文件就是字节序列,可以是任意编码的字节序列。如果在中文机器上,直接创建文本文件,该文本文件只认识ANSI编码
public static void main(String[] args) throws UnsupportedEncodingException {
        // TODO Auto-generated method stub
        
        /*
         * 在utf-8编码中中文占3个字节,而bgk编码中中文占2个字节
         */
        String s = "慕课ABC";
        byte[] byte1 = s.getBytes();
        for(byte b : byte1)  //byte 8bits, int 32 bits. xx vs xxxxxxxx
            System.out.print(Integer.toHexString(b & 0xff) + " "); //e6 85 95 e8 af be 41 42 43 (code: utf-8)
        System.out.println();
        
        byte[] byte2 = s.getBytes("gbk");
        for(byte b : byte2) 
            System.out.print(Integer.toHexString(b & 0xff) + " "); //c4 bd bf ce 41 42 43 
        
        /*
         * java是双字节编码,utf-16be编码。意思是Java里的字符串的一个字符占用2个字节 
         * 面试官会问:Java一个字符中可不可以放汉字呢?如果是gbk编码,是可以的。
         */
        System.out.println();
        byte[] byte3 = s.getBytes("utf-16be");
        for(byte b : byte3) 
            System.out.print(Integer.toHexString(b & 0xff) + " "); //61 55(慕) 8b fe(课) 0 41(A) 0 42(B) 0 43(C) 
        
        System.out.println();
        String s1 = new String(byte3);
        System.out.println(s1); //乱码
        String s2 = new String(byte3, "utf-16be");
        System.out.println(s2); //慕课ABC
        
    }
View Code

相关文章:

  • 2022-01-13
  • 2021-09-30
  • 2021-06-02
  • 2021-10-10
  • 2021-08-04
  • 2022-01-21
  • 2021-08-09
猜你喜欢
  • 2022-12-23
  • 2021-08-27
  • 2022-01-04
  • 2021-10-29
  • 2021-06-19
  • 2021-09-08
  • 2021-09-03
相关资源
相似解决方案