从网上找的,加入了屏幕读写功能。实现从中文到UTF-8(两种格式的转换)
-------------------------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class TransformToChinese
{
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("\\u" + 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) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
System.out.println(GBK2Unicode(str));
System.out.println(GBK2Unicode2(str));
}
}
输入 : “输入”
输出:
\u8f93\u5165
输入