有个关于字符串中半角转全角的处理
public class BQChange {
public static String bToQChange(String bCode){
String outStr ="";
String tmpStr ="";
byte[] b=null;
for(int i=0;i<bCode.length();i++){
tmpStr = bCode.substring(i,i+1);
if(tmpStr.getBytes().length==2){
outStr = outStr+tmpStr;
continue;
}
try {
b = tmpStr.getBytes("unicode");
if(b[3]!=-1){
b[2]=(byte)(b[2]-32);
b[3]=-1;
outStr = outStr+new String(b,"unicode");
}else{
outStr = outStr+tmpStr;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
return outStr;
}
public static void main(String[] args) throws Exception{
System.out.println(BQChange.bToQChange("(中国人)"));
}
}
我在eclipse里面运行,只有“中国人”3个字输出,但前后都有空格,看空格长度是对了,但没字符出现。
在Linux上,竟然可以输出“(中国人)”,这个为什么?

解决方案 »

  1.   


    /*全转半是对滴,成功了一大半了 xp Netbeans
    (中国人)
    65288
    40
    65289*/
            try {
                String str = bToQChange(new String("(中国人)".getBytes(System.getProperty("file.encoding")),"GBK"));
                System.out.println(str);
                System.out.println(""+(int)'(');
                System.out.println(""+(int)'(');
                System.out.println(""+(int)str.charAt(str.length()-1));
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
      

  2.   

    我试过了,这么转,看着没问题,但我的默认字符集,就是"GBK",把那个char的转成int也对,但问题是,在eclipse的控制台,显示不出来