zoukankan      html  css  js  c++  java
  • 感受一下Java乱码出现就知道怎么解决Java乱码了

    public class StringSize {
    
        public static void main(String[] args) throws Exception {
    
            System.out.println("================UTF8 ENCODE================");
            // UTF8 ENCODE 一个中文汉字 3个字节
            String testDate = "abc123哈哈";
            System.out.println(testDate.length());
            System.out.println(testDate.getBytes(StandardCharsets.UTF_8).length); // 6+6
    
            System.out.println("================GBK ENCODE================");
            //GBK ENCODE 一个中文汉字 2个字节
            testDate = "abc123哈哈";
            System.out.println(testDate.length());
            System.out.println(testDate.getBytes(Charset.forName("GBK")).length); // 6+4
            System.out.println("================ISO_8859_1 ENCODE================");
            //ISO_8859_1 ENCODE 一个中文汉字 1个字节
            System.out.println(testDate.getBytes(StandardCharsets.ISO_8859_1).length); // 6+2
    
            Charset GBK = Charset.forName("GBK");
            System.out.println("================乱码解决================");
            byte[] utf8Data = testDate.getBytes(StandardCharsets.UTF_8);
            byte[] gbkData = testDate.getBytes(GBK);
    
            System.out.println("new String(utf8Data) = " + new String(utf8Data));
            System.out.println("new String(gbkData) = " + new String(gbkData));
    
            System.out.println("new String(utf8Data,StandardCharsets.ISO_8859_1) = " + new String(utf8Data,
                    StandardCharsets.ISO_8859_1));
            System.out.println("new String(gbkData,StandardCharsets.ISO_8859_1) = " + new String(gbkData,
                    StandardCharsets.ISO_8859_1));
            System.out.println("new String(utf8Data,GBK) = " + new String(utf8Data, GBK));
            System.out.println("================乱码================");
            System.out.println("new String(utf8Data) = " + new String(utf8Data));
            System.out.println("new String(gbkData, GBK) = " + new String(gbkData, GBK));
            System.out.println("================================");
    
        }
    }
    
    输出如下:
            ================UTF8 ENCODE================
            8
            12
            ================GBK ENCODE================
            8
            10
            ================ISO_8859_1 ENCODE================
            8
            ================乱码解决================
            new String(utf8Data) = abc123哈哈
            new String(gbkData) = abc123����
            new String(utf8Data,StandardCharsets.ISO_8859_1) = abc123哈哈
            new String(gbkData,StandardCharsets.ISO_8859_1) = abc123¹þ¹þ
            new String(utf8Data,GBK) = abc123鍝堝搱
            ================乱码================
            new String(utf8Data) = abc123哈哈
            new String(gbkData, GBK) = abc123哈哈
            ================================

  • 相关阅读:
    TCP的核心系列 — SACK和DSACK的实现(一)
    Linux2.6中的Slab层
    UVA 11549 Calculator Conundrum (Floyd判圈算法)
    2013第四届蓝桥杯决赛Java高职高专组题目以及解法答案
    hdu-Common Subsequence
    UVA 10869
    【Struts2学习笔记(3)】至Action注入属性值
    【winows7+android-ndk-r9+Cygwin 】cocos2dx 2.*游戏移植Android平台完全手册
    Ubuntu 14.04 64位字体美化(使用黑文泉驿)
    Android Fragment——详细解释
  • 原文地址:https://www.cnblogs.com/leodaxin/p/15221120.html
Copyright © 2011-2022 走看看