new String(s.getBytes("ISO-8859-1"), "UTF-8")转换后还是乱码怎么回事
package javaweb1;
public class ChineseToString {
public ChineseToString() {
// TODO Auto-generated constructor stub
}
/**
* 构造方法
*/
public String toString(String s) {
String text = "";
// 判断要转码的字符串是否有效
if (!s.equals("") && !s.equals("null")) {
try {
// 将字符串进行编码处理
text = new String(s.getBytes("ISO-8859-1"), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
}
return text;
}
/**
* 转码处理字符串
*
* @param s 要转码的字符串
*
* @return 转码后的字符串
*/
public static void main(String[] args) {
System.out.println(new ChineseToString().toString("达到"));
}
}
正常显示中文
String.getBytes()JAVA编码转换的详细过程 https://wenku.baidu.com/view/fc314c32f28583d049649b6648d7c1c708a10b7b.html
package Test;
public class ChineseToString {
public ChineseToString() {
// TODO Auto-generated constructor stub
}
/**
* 构造方法
*/
public String toString(String s) {
String text = "";
// 判断要转码的字符串是否有效
if (!s.equals("") && !s.equals("null")) {
try {
// 将字符串进行编码处理
text = new String(s.getBytes("ISO-8859-1"), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
}
return text;
}
/**
* 转码处理字符串
*
* @param s 要转码的字符串
*
* @return 转码后的字符串
*/
public static void main(String[] args) {
// System.out.println(new ChineseToString().toString("达到"));
try {
String s=new String("达到".getBytes("UTF-8"),"ISO-8859-1");
System.out.println(new ChineseToString().toString(s));
} catch (Exception e) {
e.printStackTrace();
}
}
}