zoukankan      html  css  js  c++  java
  • 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); } }
  • 相关阅读:
    python并发编程的几种方法
    pycharm pytest 运行时报错 gbk
    mac m1 安装python3
    python json.dumps 打印出后为乱码 解决方法
    git ssh密匙配置
    登录接口需html中的token时,需用requests-html库
    代码服务器运行时找不到包文件位置
    mac终端使用iterm及主题 高亮
    Mac 生成项目目录树形结构
    mac 安装xcode命令行工具
  • 原文地址:https://www.cnblogs.com/inspred/p/10807233.html
Copyright © 2011-2022 走看看