字符集转换的问题
/**** 字符集的转换函数 ****/
// added by baiqing 2003.02.18
public static String charConvert(String strSource){
if (strSource == null){
return "";
}
else{
try{
//return new String(strSource.getBytes("GB2312"), "ISO-8859-1");
String str = new String(strSource.getBytes("ISO-8859-1"), "GB2312");
System.out.println(str);
return str;
}
catch (Exception ex){
System.out.println(ex.toString());
return "";
}
}
}
/**** 字符集的转换函数 ****/
// added by baiqing 2003.02.18
public static String charConvert(String strSource){
if (strSource == null){
return "";
}
else{
try{
//return new String(strSource.getBytes("GB2312"), "ISO-8859-1");
String str = new String(strSource.getBytes("ISO-8859-1"), "GB2312");
System.out.println(str);
return str;
}
catch (Exception ex){
System.out.println(ex.toString());
return "";
}
}
}
反正这样的转化出来是乱码加问号。
protected static String decode(String s,String c1,String c2) throws Exception {
StringBuffer sb = new StringBuffer(); //字节编码转换,c1为源字节编码、c2目的字节编码
for(int i=0; i<s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '+':
sb.append('+');
break;
case '%':
try {
sb.append((char)Integer.parseInt(
s.substring(i+1,i+3),16));
}catch (NumberFormatException e) {
System.out.println(e.getMessage());
}
i += 2;
break;
default:
sb.append(c);
break;
}
}
String result = sb.toString();
byte[] inputBytes = result.getBytes(c1);
return new String(inputBytes,c2);
}
你要得到自己机器上的中文编码方式encoding
然后得到的字符串(设为str)
byte[] b=str.getBytes(encoding)
String temp=new String(b)
temp格式正确
StringBuffer tmpBF = new StringBuffer("");
System.loadLibrary("scanDll");
Sample1 s1 = new Sample1();
tmpBF.append(new String(s1.comScan().getBytes("iso-8859-1"),"GBK"));
System.out.println(""+tmpBF);
}
这个问题我解决了,其实只要
System.out.println(sl.comScan().getBytes("ISO-8859-1"));就可以了,
千万不要加第二个参数,那样就又错了。
不过我又有了新的问题,就是哪个DLL返回的中文长的时候(超过10个汉字),就什么都不显示了,有谁知道这个问题???