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("" + 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();
}
}
}
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("" + 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();
}
}
}
更多精彩
赞助商链接