JAVA UTF-8 GB2312 编码互转

 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class UnicodeChange {

	//UTF-8->GB2312
	public static String utf8Togb2312(String str){ 

        StringBuffer sb = new StringBuffer(); 

        for ( int i=0; iUTF-8
	public static String gb2312ToUtf8(String str) { 

        String urlEncode = "" ; 

        try { 

            urlEncode = URLEncoder.encode (str, "UTF-8" ); 

        } catch (UnsupportedEncodingException e) { 

            e.printStackTrace(); 

        } 

        return urlEncode; 

    }
	

	public static void main(String[] args){   
	       	        
	    	try {
			
	    	String str="輸入簡體字,點下面繁體字按鈕進行在線轉換.";//%E4%B8%AD%E5%9B%BD
	    		
	    	str=gb2312ToUtf8(str);
	    		
			System.out.println(str);
				  
			str=utf8Togb2312(str);
				
			System.out.println(str);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	        
	    }   
	


}

你可能感兴趣的:(JAVA)