package cn.com.tiros.common.util;
public class ChConvert {
/**
* 半角→全角
*
* @param str
* @return
*/
public static String toDBCS(String str) {
if (str == null) {
return "";
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if ((c >= 'a') && (c <= 'z')) {
c = (c + 'a') - 'a';
} else if ((c >= 'A') && (c <= 'Z')) {
c = (c + 'A') - 'A';
} else if ((c >= '0') && (c <= '9')) {
c = (c + '0') - '0';
}
sb.append((char) c);
}
return sb.toString();
}
/**
* 全角→半角
*
* @param str
* @return
*/
public static String toSBCS(String str) {
if (str == null) {
return "";
}
StringBuffer sb = new StringBuffer();
for (int i = 0, n = str.length(); i < n; i++) {
int c = str.charAt(i);
if ((c >= 'A') && (c <= 'Z')) {
c = (c + 'A') - 'A';
} else if ((c >= '0') && (c <= '9')) {
c = (c + '0') - '0';
} else if ((c >= 'a') && (c <= 'z')) {
c = (c + 'a') - 'a';
}
sb.append((char) c);
}
return sb.toString();
}
public static void main(String[] args) {
String str = "天山路8号";//半角
System.out.println(ChConvert.toDBCS(str));
System.out.println(ChConvert.toSBCS(ChConvert.toDBCS(str)));
}
}