import java.io.UnsupportedEncodingException; public class FullCharConverter { public static void main(String[] args) throws UnsupportedEncodingException { // 全角转半角 String qjStr = "test!! 全角转 半角,,DAO 53232 "; String result = full2HalfChange(qjStr); System.out.println(qjStr); System.out.println(result); System.out.println("------------------------------------"); // 半角转全角 String bjStr = "java 半角 转全角 2345"; System.out.println(bjStr); System.out.println(half2Fullchange(bjStr)); } // 全角转半角的 转换函数 public static String full2HalfChange(String QJstr) throws UnsupportedEncodingException { StringBuffer outStrBuf = new StringBuffer(""); String tStr = ""; byte[] b = null; for (int i = 0; i < QJstr.length(); i++) { tStr = QJstr.substring(i, i + 1); // 全角空格转换成半角空格 if (tStr.equals(" ")) { outStrBuf.append(" "); continue; } b = tStr.getBytes("unicode"); // 得到 unicode 字节数据 if (b[2] == -1) { // 表示全角? b[3] = (byte) (b[3] + 32); b[2] = 0; outStrBuf.append(new String(b, "unicode")); } else { outStrBuf.append(tStr); } } return outStrBuf.toString(); } // 半角转全角 public static String half2Fullchange(String QJstr) throws UnsupportedEncodingException { StringBuffer outStrBuf = new StringBuffer(""); String Tstr = ""; byte[] b = null; for (int i = 0; i < QJstr.length(); i++) { Tstr = QJstr.substring(i, i + 1); if (Tstr.equals(" ")) { // 半角空格 outStrBuf.append(Tstr); continue; } b = Tstr.getBytes("unicode"); if (b[2] == 0) { // 半角? b[3] = (byte) (b[3] - 32); b[2] = -1; outStrBuf.append(new String(b, "unicode")); } else { outStrBuf.append(Tstr); } } return outStrBuf.toString(); } }