编码的程序是String tempS = new String(resutlBytesData[p], "UTF-16BE");//得到的tempS就是乱码了
byte[] btmp = tempS.getBytes("GBK");
System.out.println(Base64.encode(btmp));
然后我解码的顺序是byte[] b1 = Base64.decode(s1);
String ss1 = new String(b1,"GBK");
byte[] bb1 = ss1.getBytes("UTF-16BE");
bb1解出来后是resutlBytesData[p],但是不对?哪里出错了吗?编码乱码
byte[] btmp = tempS.getBytes("GBK");
System.out.println(Base64.encode(btmp));
然后我解码的顺序是byte[] b1 = Base64.decode(s1);
String ss1 = new String(b1,"GBK");
byte[] bb1 = ss1.getBytes("UTF-16BE");
bb1解出来后是resutlBytesData[p],但是不对?哪里出错了吗?编码乱码
public class anotherTest {
public static void main(String[] args) throws UnsupportedEncodingException {
byte[] bytes = {6, 8, 4, 0, 1, 2, 1, 117, 49, 78, -114, 78, 14, 123, 44, 78, 9, 101, -71, 84, 8, 79, 92, -116, 3, 101, 116, -1, 12, 102, -126, 80, 92, -117, -35, -115, 57, 101, 47, 78, -40, 81, 81, 99, 98, 98, 75, 103, 58, 101, 47, 78, -40, 117, 53, 91, 80, 82, 56, -1, 8, 84, 43, 81, 108, 78, -92, 78, 0, 83, 97, -112, 26, 117, 53, 91, 80, 82, 56, -1, 9, 78, 26, 82, -95, 48, 2, -117, -9, 96, -88, -112, 9, 98, -23, 81, 118, 78, -42, 83, -17, -115, 45, 78, 112, 78, -89, 84, -63, 48, 2, -117, -35, -115, 57, 101, 47, 78, -40, 83, -17, -115, 45, 78, 112, 118, -124};
System.out.println("解码前:"+Arrays.toString(bytes));
String tempS = new String(bytes, "UTF-16BE");//得到的tempS就是乱码了
byte[] btmp = tempS.getBytes("GBK");
String result = Base64.encode(btmp); byte[] rBtemp = Base64.decode(result);
String rTempS = new String(rBtemp,"GBK");
byte[] rBytes = rTempS.getBytes("UTF-16BE");
System.out.println("解码后:"+Arrays.toString(rBytes));
}
}
我不明白你想实现什么:
下面是我的一个字符转为字节数组 转为16进制字符 然后转回的例子。:public static void main(String[] args) {
String str = hexString("好啊tianta了");
System.out.println(str);
byte[] b = HexString2Bytes(str);
try {
String s=new String(b,"GB2312");
System.out.println(s);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static String hexString(String str) {
String ret = "";
byte[] b;
try {
b = str.getBytes("GB2312");
for (int i = b.length - 1; i >= 0; i--) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return ret;
} public static byte[] HexString2Bytes(String hexstr) {
byte[] b = new byte[hexstr.length() / 2];
int j = 0;
for (int i = b.length-1; i >=0; i--) {
char c0 = hexstr.charAt(j++);
char c1 = hexstr.charAt(j++);
b[i] = (byte) ((parse(c0) << 4) | parse(c1));
}
return b;
} private final static byte[] hex = "0123456789ABCDEF".getBytes(); private static int parse(char c) {
if (c >= 'a')
return (c - 'a' + 10) & 0x0f;
if (c >= 'A')
return (c - 'A' + 10) & 0x0f;
return (c - '0') & 0x0f;
}
首先第一个问题你的 bytes 是哪里来的?它本身的字符编码是什么?我先假设你的 Bytes 来源没问题,这些Bytes是通过 UTF-16BE 编码的 字符。
你的想法是:
先
UTF-16BE 编码的Bytes -(1 UTF-16BE 解码)-》字符-(2 GBK 编码) -》GBK 编码的Bytes
后
GBK 编码的Bytes -(3 GBK 解码)-》字符 -(4 UTF-16BE 编码)-》得到最初的Bytes当中省略了Base64编码,这个你显然是为了显示方便,这步是没问题的。 问题处在 2, UTF-16 的 码点数(可表达字符) 大于 GBK,
所以会存在某些 无法用 GBK 编码的码点,
这些码点在 经过GBK编码后,在Bytes中就不存在了,或者以错误的形式存在。
所以 经过3 和 4 得到的 Bytes 和初始的会不一样。