zoukankan      html  css  js  c++  java
  • Java Charset, getBytes()

    package com.cg.test;
    
    import java.nio.charset.Charset;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    
    import org.apache.commons.codec.binary.Hex;
    
    public class HexString {
    
        /**
         * @param args
         */
        public static void main(String[] args) throws Exception {
    	
    	String s="1a国";
    	System.out.println("原始字符串:"+s);
    	
    	System.out.println(s.getBytes("ASCII").length);
    	String sEncoded = Hex.encodeHexString(s.getBytes("ASCII"));
    	System.out.println("ASCII: "+sEncoded);
    	
    	sEncoded = Hex.encodeHexString(s.getBytes("GBK"));
    	System.out.println("GBK: "+sEncoded);
    
    	System.out.println("Default charset: "+Charset.defaultCharset().name());
    	sEncoded = Hex.encodeHexString(s.getBytes());
    	System.out.println("Default: "+sEncoded);
    	//output:3161e59bbd
    
    	//BOM: Byte Order Mark 字节序标记
    	sEncoded = Hex.encodeHexString(s.getBytes("UNICODE"));
    	System.out.println("UNICODE: "+sEncoded);
    //	output: feff0031006156fd
    	
    	sEncoded = Hex.encodeHexString(s.getBytes("UTF-16"));
    	System.out.println("UTF-16: "+sEncoded);
    	//output: feff0031006156fd
    	
    	sEncoded = Hex.encodeHexString(s.getBytes("x-UTF-16LE-BOM"));
    	System.out.println("x-UTF-16LE-BOM: "+sEncoded);
    	//output: fffe31006100fd56
    	
    
    	sEncoded = Hex.encodeHexString(s.getBytes("UTF-16BE"));
    	System.out.println("UTF-16BE: "+sEncoded);
    	//output: 0031006156fd
    	
    	sEncoded = Hex.encodeHexString(s.getBytes("UTF-16LE"));
    	System.out.println("UTF-16LE: "+sEncoded);
    	//output: 31006100fd56
    	
    	System.out.println("返回当前的 JVM 中可以使用的字符集");
    	 Map m=Charset.availableCharsets();
             Set set=m.keySet();
             Iterator it=set.iterator();
             while(it.hasNext()){
                    System.out.println(it.next());
             }
             
        }
    
    }
    

      

  • 相关阅读:
    个人总结11
    进度条4,5
    对vivo自带输入法的评价
    个人总结10
    hdu5589Tree
    HDU5213 Lucky
    2018icpcShenYangE
    bzoj3683: Falsita
    cf396C. On Changing Tree
    cf600E. Lomsat gelral
  • 原文地址:https://www.cnblogs.com/wucg/p/2579727.html
Copyright © 2011-2022 走看看