zoukankan      html  css  js  c++  java
  • java 文件的编码 问题

    package com.io;
    
    public class Encodedemo {
    
        public static void main(String[] args)throws Exception{
            // TODO Auto-generated method stub
            String s ="中国ABC";
            byte[] bytes1 = s.getBytes();//转换成字节序列用的是项目默认的字节编码
            for(byte b:bytes1){
                //把字节转换成int 以16进制的方式显示
                System.out.print(Integer.toHexString(b&0xff)+" ");
            }
            System.out.println();
            byte[] bytes2 = s.getBytes("gbk");
            //gbk编码中文占用2个字节,英文占用1个字节
            for(byte b:bytes2){
                //把字节转换成int 以16进制的方式显示
                System.out.print(Integer.toHexString(b&0xff)+" ");
            }
            
            System.out.println();
            byte[] bytes3 = s.getBytes("utf-8");
            //utf-8编码中文占用3个字节,英文占用1个字节
            for(byte b:bytes3){
                //把字节转换成int 以16进制的方式显示
                System.out.print(Integer.toHexString(b&0xff)+" ");
            }
            //java 是双字节编码 utf-16be
            //utf-16be中文占用2个字节,英文占用2个字节
            System.out.println();
            byte[] bytes4 = s.getBytes("utf-16be");
            //utf-8编码中文占用3个字节,英文占用1个字节
            for(byte b:bytes4){
                //把字节转换成int 以16进制的方式显示
                System.out.print(Integer.toHexString(b&0xff)+" ");
            }
            System.out.println();
            /*
             * 当你的字节序列是某种编码是,这个时候想把字节序列变成
             * 字符串,也需要用这种编码方式,否者会出现乱码
             */
            String str1 = new String(bytes4);
            System.out.println(str1);
            String str2 = new String(bytes4,"utf-16be");
            System.out.println(str2);
            /*
             * 文本文件 就是字节序列
             * 可以是任意编码的字节序列
             * 如果我们在中文机器上直接创建文本文件  直接创建
             * 那么该文本文件只认识ANSI编码
             * 联通、联这是一种巧合,他们正好符合了utf-8编码的规制
             */
        }
        
    
    }
  • 相关阅读:
    USBkiller 破解绿色版 by TK
    北京理工大学信息安全与对抗竞赛crackme02分析
    EnMp3Player 破解
    IDA相关下载
    猜数游戏 by TK QQ:86935367
    mp3播放器 by TK QQ:86935367
    UPX Easy GUI 汉化版by TK
    superdic cracked by TK
    vc 获取机器码
    (转)UITableView使用
  • 原文地址:https://www.cnblogs.com/loveincode/p/4491132.html
Copyright © 2011-2022 走看看