1. String转ASCII码
public static String stringToAscii(String value) {
StringBuffer sbu = new StringBuffer();
char[] chars = value.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (i <= chars.length - 1) {
int ii = chars[i];
sbu.append(Integer.toHexString(ii));
} else {
sbu.append((int) chars[i]);
}
}
return sbu.toString();
}
2. ASCII码转String
public static String asciiToString(String value) {
if("".equals(value) || value == null){
return "";
}
StringBuffer sBuffer = new StringBuffer();
char[] chars = value.toCharArray();
for (int i = 0; i < chars.length; i = i + 2) {
if (i < chars.length - 1) {
int ii = Integer.valueOf(
Character.toString(chars[i]) + Character.toString(chars[i + 1]), 16);
sBuffer.append((char) (ii));
}
}
return sBuffer.toString();
}
3. 输入流转字节数组
public static String inputStream2Byte(InputStream inputStream) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.close(); return new String(bos.toByteArray(), "UTF-8"); }
4. 输入流转字符串
public static String InputStreamTOString(InputStream in) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int count = -1; while ((count = in.read(data, 0, 2048)) != -1) outStream.write(data, 0, count); data = null; return new String(outStream.toByteArray()); }
5. 判断输入是不是包含中文
public static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) { return true; } return false; } public static boolean isChinese(String strName) { char[] ch = strName.toCharArray(); for (int i = 0; i < ch.length; i++) { char c = ch[i]; if (isChinese(c)) { return true; } } return false; }
6. 检验子网掩码的合法性:子网掩码转化为二进制之后不能全是1,也不能全是0,必须是连续的1或者连续的0,也就是说1和0不能交替出现。
实现方式,先将4个整数字段按照每八位拼接起来,软后转化为二进制显示方式,使用正则表达式进行匹配。
public static boolean checkMask(String maskStr){ String[] ips = maskStr.split("\\."); String binaryVal = ""; for (int i = 0; i < ips.length; i++) { String binaryStr = Integer.toBinaryString(Integer.parseInt(ips[i])); Integer times = 8 - binaryStr.length(); for(int j = 0; j < times; j++) { binaryStr = "0" + binaryStr; //补齐八位,每次需要进行八位合并 } binaryVal += binaryStr; } Pattern regxPattern = Pattern.compile("^[1]*[0]*$"); if(regxPattern.matcher(binaryVal).matches()) { return true; }else { return false; } }
7. 检查IP的合法性,并转化为整数表示
public static boolean validIP(String ip) { if (ip == null) { return false; } String IP_REGEX = "\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b"; Pattern ipPattern = Pattern.compile(IP_REGEX); Matcher matcher = ipPattern.matcher(ip); return matcher.matches(); } public static Integer ipString2Int(String strIp){ if(!validIP(strIp)) return null; int[] ip = new int[4]; String[] ips = strIp.split("\\."); ip[0] = Integer.parseInt(ips[0]); ip[1] = Integer.parseInt(ips[1]); ip[2] = Integer.parseInt(ips[2]); ip[3] = Integer.parseInt(ips[3]); return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]; }
原文:http://www.cnblogs.com/zhuyp1015/p/4345703.html