这是一个可以从乱码文本中得到正确的原始文本的程序,其基于的原理在于错误的编码往往导致位补充,因此正确的文本使用的字节数应该是最少的(之一)。
复制代码 代码如下:
package com.hongyuan.test;
import java.io.UnsupportedEncodingException;
public class CharSetTest {
public static final String[] CHARSET_NAMES=new String[]{"ISO8859-1","GBK","UTF-8"};
public static void main(String[] args) throws UnsupportedEncodingException {
//乱码字符串
String str="寰蒋鐧惧害鍏辨帹Windows XP鑱斿悎闃叉姢瑙e喅鏂规";
int strLength=Integer.MAX_VALUE; //字符长度
String newStr=""; //从乱码字符串分析出的字符串
String srcCharSet=""; //当前乱码字符串编码
String targetCharSet=""; //乱码字符串正确的编码
//遍历可能的编码组合,从中造成编码长度最小的编码格式
for(int i=0;i
//System.out.println(temp);
if(temp.length()<=strLength){
strLength=temp.length();
newStr=temp;
srcCharSet=CHARSET_NAMES[i];
targetCharSet=CHARSET_NAMES[j];
}
}
}
//输出查询到的编码及正确文本格式
System.out.println(srcCharSet+"-->"+targetCharSet+":"+newStr);
}
}



