java utf8 gb2312_JAVA UTF-8 GB2312 编码互转

  • Post author:
  • Post category:java


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; i

char c = str.charAt(i);

switch (c) {

case ‘+’ :

sb.append( ‘ ‘ );

break ;

case ‘%’ :

try {

sb.append(( char )Integer.parseInt (

str.substring(i+1,i+3),16));

}

catch (NumberFormatException e) {

throw new IllegalArgumentException();

}

i += 2;

break ;

default :

sb.append(c);

break ;

}

}

String result = sb.toString();

String res= null ;

try {

byte [] inputBytes = result.getBytes( “8859_1” );

res= new String(inputBytes, “UTF-8” );

}

catch (Exception e){}

return res;

}

//GB2312->UTF-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();

}

}

}



版权声明:本文为weixin_39616547原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。