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); } }

猜你喜欢

转载自www.cnblogs.com/inspred/p/10807233.html