转载:
package cn.com.wap.util;
/**
*
* @作者 meriny
* @日期 Jun 17, 2010 9:19:15 AM
* 将ASCII编码格式的中文转化为gbk格式的中文
*/
public class AsciiUtil {
public static String ascii2native(String str) {
String hex = "0123456789ABCDEF";
StringBuffer buf = new StringBuffer();
int ptn = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '\\' && i + 1 <= str.length() && str.charAt(i + 1) == '\\') {
buf.append("\\\\");
i += 1;
} else if (c == '\\' && i + 6 <= str.length() && str.charAt(i + 1) == 'u') {
String sub = str.substring(i + 2, i + 6).toUpperCase();
int i0 = hex.indexOf(sub.charAt(0));
int i1 = hex.indexOf(sub.charAt(1));
int i2 = hex.indexOf(sub.charAt(2));
int i3 = hex.indexOf(sub.charAt(3));
if (i0 < 0 || i1 < 0 || i2 < 0 || i3 < 0) {
buf.append("\\u");
i += 1;
} else {
byte[] data = new byte[2];
data[0] = i2b(i1 + i0 * 16);
data[1] = i2b(i3 + i2 * 16);
try {
buf.append(new String(data, "UTF-16BE").toString());
} catch (Exception ex) {
buf.append("\\u" + sub);
}
i += 5;
}
} else {
buf.append(c);
}
}
return buf.toString();
}
@SuppressWarnings("unused")
private static int b2i(byte b) {
return (int) ((b < 0) ? 256 + b : b);
}
private static byte i2b(int i) {
return (byte) ((i > 127) ? i - 256 : i);
}
}