关于java的String的编码的各种情况测试代码

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collection;

import javax.xml.bind.DatatypeConverter;

import org.apache.commons.io.FileUtils;

import com.google.common.io.BaseEncoding;

public class testCmd {
	public static boolean isString(String sourceString, String codec) 
	{
		  byte[]     temp;  
		  try 
		  {
			  temp  =   sourceString.getBytes(codec);
			  String   compstring;  
			  compstring   =   new   String(temp);   
			  if(sourceString.equals(compstring))  
			  {  
			        return true;  
			  }
		  } 
		  catch (UnsupportedEncodingException e) 
		  {
			e.printStackTrace();
		  }  
		  return false;
	}
	
	public static String guessEncoding(byte[] bytes) 
	{  
	    String DEFAULT_ENCODING = "UTF-8";  
	    org.mozilla.universalchardet.UniversalDetector detector =  
	        new org.mozilla.universalchardet.UniversalDetector(null);  
	    detector.handleData(bytes, 0, bytes.length);  
	    detector.dataEnd();  
	    String encoding = detector.getDetectedCharset();  
	    detector.reset();  
	    if (encoding == null) {  
	        encoding = DEFAULT_ENCODING;  
	    }  
	    return encoding;  
	}
	public static void main(String[] args) throws IOException
	{
		System.out.println("start");
		byte[] arr= {(byte) 0x31,(byte) 0xb9,(byte) 0xfe};
		String str=new String(arr,"gbk");
		
		System.out.println("编码:"+guessEncoding(arr));
		System.out.println("编码:"+guessEncoding(str.getBytes("gbk")));
		System.out.println("编码:"+guessEncoding(str.getBytes("utf-8")));
		System.out.println("编码:"+guessEncoding(str.getBytes("unicode")));
		if(isString(str,"utf-8"))
		{
			System.out.println("utf-8:"+str);
		}
		
		if(isString(str,"gbk"))
		{
			System.out.println("gbk:"+str);
		}
		
        byte[] packArray=str.getBytes();
        String strHex = DatatypeConverter.printHexBinary(packArray);  
        System.out.println("send data:"+strHex);
        
        packArray=str.getBytes("utf-8");
        strHex = DatatypeConverter.printHexBinary(packArray);  
        System.out.println("send data:"+strHex);
        
        packArray=str.getBytes("gbk");
        strHex = DatatypeConverter.printHexBinary(packArray);  
        System.out.println("send data:"+strHex);
        
        byte[] arr1= {(byte) 0x54,(byte) 0xc8};
        String str2=new String(arr1,"unicode");
        System.out.println("编码2:"+guessEncoding(arr1));
        System.out.println("编码2:"+guessEncoding(str2.getBytes("utf-8")));
        System.out.println("编码2:"+guessEncoding(str2.getBytes("unicode")));
        packArray=str2.getBytes("unicode");
        strHex = DatatypeConverter.printHexBinary(packArray);  
        System.out.println("send data:"+strHex);
        
        packArray=str2.getBytes("utf-8");
        strHex = DatatypeConverter.printHexBinary(packArray);  
        System.out.println("send data:"+strHex);
        
        packArray=str2.getBytes("gbk");
        strHex = DatatypeConverter.printHexBinary(packArray);  
        System.out.println("send data:"+strHex);
        
        String str3="123哈";
        packArray=str3.getBytes();
        System.out.println("编码3:"+guessEncoding(str3.getBytes("gb2312")));
        strHex = DatatypeConverter.printHexBinary(packArray);  
        System.out.println("send data:"+strHex);
        
        packArray=str3.getBytes("utf-8");
        strHex = DatatypeConverter.printHexBinary(packArray);  
        System.out.println("send data:"+strHex);
        
        String strt="3C61707069643D38653238333500";
        byte[] pa=BaseEncoding.base16().decode((String)strt);
        System.out.println("编码3:"+guessEncoding(pa));
        
        String strt1="3C617000";
        byte[] pa1=BaseEncoding.base16().decode((String)strt1);
        System.out.println("编码33:"+guessEncoding(pa1));
        
        byte[] arr3= {(byte) 0x31,(byte) 0xE5,(byte) 0x93,(byte) 0x88};
        String str4=new String(arr3);
        System.out.println("send data:"+str4);
        System.out.println("编码2:"+guessEncoding(arr3));
        System.out.println("编码2:"+guessEncoding(str4.getBytes("utf-8")));
        System.out.println("编码2:"+guessEncoding(str4.getBytes("unicode")));
        packArray=str4.getBytes();
        strHex = DatatypeConverter.printHexBinary(packArray);  
        System.out.println("send data:"+strHex);
        System.out.print("OK");
	}
}


start
编码:WINDOWS-1252
编码:WINDOWS-1252
编码:UTF-8
编码:UTF-16BE
utf-8:1哈
send data:31E59388
send data:31E59388
send data:31B9FE
编码2:WINDOWS-1252
编码2:UTF-8
编码2:UTF-16BE
send data:FEFF54C8
send data:E59388
send data:B9FE
编码3:WINDOWS-1252
send data:313233E59388
send data:313233E59388
编码3:UTF-8
编码33:UTF-8
send data:1哈
编码2:UTF-8
编码2:UTF-8
编码2:UTF-16BE
send data:31E59388
OK

你可能感兴趣的:(其他日志)