• IO系列之二:编码


    一,gbk编码,中文占用2个字节,英文占用1个字节

    package com.amazing.jdk.learn2IO_0821.encode;
    
    import java.io.UnsupportedEncodingException;
    
    /**
     * Created by yaming on 17-8-21.
     * 编码
     */
    public class EncodeDemo {
        public static void main(String[] args) throws UnsupportedEncodingException {
            String s="慕课ABC";
            byte[] bytes1=s.getBytes();//转换成字节序列用的是项目指定的编码utf-8
            for (byte b:bytes1){
                //把字节(转换成了int)以16进制的方式显示
                System.out.print(Integer.toHexString(b & 0xff)+" ");
    
            }
            System.out.println();
            byte[] bytes2=s.getBytes("gbk");//转换成字节序列时,指定编码为gbk
            for (byte b:bytes2){
                System.out.print(Integer.toHexString(b & 0xff)+" ");
            }
           
        }
    }

    二,utf-8编码中文占用3个字节,英文占用1个字节。

    package com.amazing.jdk.learn2IO_0821.encode;
    
    import java.io.UnsupportedEncodingException;
    
    /**
     * Created by yaming on 17-8-21.
     * 编码
     */
    public class EncodeDemo {
        public static void main(String[] args) throws UnsupportedEncodingException {
            String s="慕课ABC";
            byte[] bytes1=s.getBytes();//转换成字节序列用的是项目指定的编码utf-8
            for (byte b:bytes1){
                //把字节(转换成了int)以16进制的方式显示
                System.out.print(Integer.toHexString(b & 0xff)+" ");
    
            }
            System.out.println();
            byte[] bytes2=s.getBytes("utf-8");//转换成字节序列时,指定编码为gbk
            for (byte b:bytes2){
                System.out.print(Integer.toHexString(b & 0xff)+" ");
            }
            
        }
    }

    三,java是双字节编码 utf-16be编码

    utf-16be 中文占用两个字节,英文占用两个字节

    Java里一个字符占两个字节。Java里的一个字符能不能放一个汉字?  可以的,默认JDK的汉字是占两个字节的。

    package com.amazing.jdk.learn2IO_0821.encode;
    
    import java.io.UnsupportedEncodingException;
    
    /**
     * Created by yaming on 17-8-21.
     * 编码
     */
    public class EncodeDemo {
        public static void main(String[] args) throws UnsupportedEncodingException {
            String s="慕课ABC";
            byte[] bytes1=s.getBytes();//转换成字节序列用的是项目指定的编码utf-8
            for (byte b:bytes1){
                //把字节(转换成了int)以16进制的方式显示
                System.out.print(Integer.toHexString(b & 0xff)+" ");
    
            }
          
            System.out.println();
            byte[]bytes3=s.getBytes("utf-16be");//使用utf-16be编码
            for(byte b:bytes3){
               System.out.print(Integer.toHexString(b & 0xff)+" ");
            }
            
        }
    }

    4,当你的字节序列是某种编码时,这个时候想把字节序列变成字符串,也需要使用这种编码方式,否则会出现乱码

    package com.amazing.jdk.learn2IO_0821.encode;
    
    import java.io.UnsupportedEncodingException;
    
    /**
     * Created by yaming on 17-8-21.
     * 编码
     */
    public class EncodeDemo {
        public static void main(String[] args) throws UnsupportedEncodingException {
            String s="慕课ABC";
            byte[] bytes1=s.getBytes();//转换成字节序列用的是项目指定的编码utf-8
            for (byte b:bytes1){
                //把字节(转换成了int)以16进制的方式显示
                System.out.print(Integer.toHexString(b & 0xff)+" ");
    
            }
            
            System.out.println();
         byte[]bytes3=s.getBytes("utf-16be");//使用utf-16be编码
    String str1
    =new String(bytes3);//用项目默认的编码(gbk),把字节序列变成字符串会出现乱码 System.out.println(str1); System.out.println(); String str2=new String(bytes3,"utf-16be"); System.out.println(str2); } }
  • 相关阅读:
    [sql]在case语句中不同情况下then的数据的数据类型不一致ORA-00932: inconsistent datatypes: expected NUMBER got CHAR
    环境迁移 小记
    linux下安装oracle遇到的问题
    正向代理与反向代理
    文件夹与SVN脱离关系
    shell 脚本中$$,$#,$?
    在MySQL中单列索引和联合索引的区别
    Java中Map、HashMap、LinkedHashMap、TreeMap的区别
    Error、Exception与RuntimeException的区别
    设计模式--单例模式
  • 原文地址:https://www.cnblogs.com/inspred/p/10807233.html
Copyright © 2020-2023  润新知