zoukankan      html  css  js  c++  java
  • Java中的编码

     1 package coreJava;
     2 
     3 import javax.swing.plaf.synth.SynthSpinnerUI;
     4 
     5 public class EncodeDemo {
     6 
     7     public static void main(String[] args) throws Exception {
     8         // TODO Auto-generated method stub
     9 
    10         String s = "慕课ABC";
    11         byte[] bytes1 = s.getBytes();//转换成字节序列用的是项目默认的编码gbk
    12         for(byte b: bytes1){
    13             //把字节转换成(int)以十六进制的方式显示
    14             System.out.print(Integer.toHexString(b & 0xff)+" ");
    15             
    16         }
    17         
    18         System.out.println();
    19         byte[] bytes2 = s.getBytes("gbk");
    20         for(byte b:bytes2){
    21             System.out.print(Integer.toHexString(b & 0xff)+" ");
    22             //gbk编码,中文占两个字节,英文占一个字节
    23         }
    24         
    25         System.out.println();
    26         byte[] bytes3 = s.getBytes("utf-8");
    27         for(byte b: bytes3){
    28             //utf-8编码中文占用三个字节,英文占用一个字节
    29             System.out.print(Integer.toHexString(b & 0xff)+" ");
    30         }
    31         
    32         //Java是双字节编码 utf-16be:(中文和英文都占用两个字节)
    33         byte[] bytes4 =s.getBytes("utf-16be");
    34         for(byte b: bytes4){
    35             System.out.print(Integer.toHexString(b & 0xff)+" ");
    36         }
    37         System.out.println();
    38         /**
    39          * 当你的字节序列是某种编码时,这个时候想把字节序列变成字符串
    40          * 也需要用这种编码方式,否则会出现乱码
    41          */
    42         String str = new String(bytes4);//用项目默认的编码格式
    43         System.out.println(str);
    44         String str2 = new String(bytes4,"utf-16be");
    45         System.out.println(str2);
    46         
    47         /**
    48          * 文本文件放的就是字节序列,
    49          * 可以使任意编码的字节序列
    50          * 我们可以在中文机器上直接创建文本文件,那么该文本文件只认识ansi编码
    51          * 联通,联这是一种巧合,他们符合了utf-8的编码规则。
    52          */
    53     }
    54 
    55 }
  • 相关阅读:
    【class2src】Decompiler
    【JAVA-JDT-AST】Java抽象语法树的构建、遍历及转成dot格式(附Github源码)
    【IBM-WALA】Step by Step : use WALA to generate System Dependency Graph PDF and Dot File (Mac)
    [R]统计工具包
    [PYTHON-TSNE]可视化Word Vector
    【Latex】常用工具包
    [Python]编程之美
    【python】用python生成pdf文件
    【python】并查集
    【Scikit】实现Multi-label text classification代码模板
  • 原文地址:https://www.cnblogs.com/blogofwyl/p/4724113.html
Copyright © 2011-2022 走看看