import org.apache.log4j.Logger;
public class ConvertCoding {
static Logger log = Logger.getLogger(ConvertCoding.class);
public static String gb2unicode(final String gbkstr)
{
try {
String tmp;
StringBuffer sb = new StringBuffer(500);
char c;
int i, j;
sb.setLength(0);
for(i = 0;i<gbkstr.length();i++){
c = gbkstr.charAt(i);
if (c > 255) {
j = (c >>> 8);
tmp = Integer.toHexString(j);
if (tmp.length() == 1) sb.append("0");
sb.append(tmp);
j = (c & 0xFF);
tmp = Integer.toHexString(j);
if (tmp.length() == 1) sb.append("0");
sb.append(tmp);
}else{
sb.append("00"+Integer.toHexString(c));
}
}
return new String(sb).toUpperCase();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public static String unicode2gb( final String dataStr)
{
try{
int totalnum = dataStr.length();
if(totalnum%4!=0)
{
throw new Exception("非符合条件的unicode码!");
}
StringBuffer buffer = new StringBuffer();
String charStr = "";
char letter = ' ';
for(int i=0 ;i<totalnum;i=i+4){
charStr = dataStr.substring(i,i+4);
letter = (char) Integer.parseInt(charStr,16);
buffer.append(new Character(letter).toString());
}
return buffer.toString();
}catch(Exception e){
e.printStackTrace();
return "";
}
}
public static void main(String[] args) {
}
}