首页 > 学院 > 开发设计 > 正文

java GB转 UTF-8字符

2019-11-17 03:59:42
字体:
来源:转载
供稿:网友
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();
  }
  
}

}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表