判断字符串是否使用ISO-8859-1码表
如果是,就转成utf-8编码
try {
//解决乱码问题, 判断当前字符串是否是使用ISO-8859-1码表, 如果不是就不会走到if里
String title = new String("哈哈".getBytes(), "ISO-8859-1");
Log.e("----TAG", "title: " + title);
if (title.equals(new String(title.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
String result = new String(title.getBytes("ISO-8859-1"), "utf-8");
Log.e("----TAG", "result: " + result);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e("----TAG", "Exception: " + e);
}
结论:
如果一个字符串使用不是原来的编码转换 new String(title.getBytes(“ISO-8859-1”), “ISO-8859-1”)
先转成字节数组, 在转成字符串是转不回来的, (当然我说的是中文)
为什么英文就可以转回来, 因为UTF-8和GBK都兼容ISO-8859-1
所以 new String( str.getBytes( “utf8” ), “utf8” ) === str,即完全可逆,
new String( str.getBytes( “ISO-8859-1” ), “ISO-8859-1” ) === str,不完全可逆。