import java.util.Scanner;
import java.io.UnsupportedEncodingException;
public class Main {
//"A"=>a3c1
private static String GbkToString(String result) throws UnsupportedEncodingException {
byte[] strChar=result.getBytes("GBK");
String str ="";
boolean doublef=true;
for(int i=0;i< strChar.length;i++){
str+=Integer.toHexString(Byte.toUnsignedInt(strChar[i]));//单字节转化成无符号
//整数(10进制),再转化成16进制字符串"a3"
doublef=!doublef;
if(doublef){
str+=" ";
}
}
return str;
}
//a3c1=>"A"
public static String stringToGbk(String string) throws Exception {
byte[] bytes = new byte[(int)(string.length() / 2.5)];
for (int i = 0; i < bytes.length/2; i++) {
byte high1 = Byte.parseByte(string.substring(i*5, i*5+1), 16);//a
byte high2 = Byte.parseByte(string.substring(i*5+1, i*5+ 2), 16);//3
bytes[2*i] = (byte) (high1 << 4 | high2);
byte low1 = Byte.parseByte(string.substring(i*5+2, i*5+3), 16);//c
byte low2 = Byte.parseByte(string.substring(i*5+3, i*5+4), 16);//1
bytes[2*i+1] = (byte) (low1 << 4 | low2);
}
String result = new String(bytes, "gbk");
return result;
}
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
String origin=sc.nextLine();
String trans = GbkToString(origin);
String result = stringToGbk(trans);
System.out.println(result);
System.out.println(trans);
}
}
可以用来将普通文本转化成GB2312码,用空格隔开。