WEB开发网
开发学院WEB开发Jsp java GB转 UTF-8字符 阅读

java GB转 UTF-8字符

 2009-12-04 21:06:43 来源:WEB开发网   
核心提示:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class GB2UTF { public static String GBK2Unicode(String str) {St
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class GB2UTF
{

public static String GBK2Unicode(String str)
{
 StringBuffer result = new StringBuffer();
 for (int i = 0; i < str.length(); i++)
 {
  char chr1 = (char)str.charAt(i);
  if(!isNeedConvert(chr1))
  {
  result.append(chr1);
  continue;
  }
  result.append("&#x" + Integer.toHexString((int)chr1) + ";");      
 }
 return result.toString();
}

public static boolean isNeedConvert(char para)
{
 return ((para&(0x00FF))!=para);
}

public static String GBK2Unicode2(String str)
{
 StringBuffer result = new StringBuffer();
 for (int i = 0; i < str.length(); i++)
 {
  char chr1 = (char)str.charAt(i);
  result.append("&#" + Integer.toString((int)chr1)+ ";"); 
 }     
 return result.toString();
}


public static void main(String[] args)
{
 try
 {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String str = br.readLine();
  System.out.PRintln(GBK2Unicode(str));
  System.out.println(GBK2Unicode2(str));
 }
 catch (IOException e)
 {
  e.printStackTrace();
 }
 
}

}

Tags:java GB UTF

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接