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();
}
if("".equals(value) || value == null){
return "";
}
char[] chars = value.toCharArray();
for (int i = 0; i < chars.length; i = i + 2) {
if (i < chars.length - 1) {
sBuffer.append((char) (ii));
}
}
return sBuffer.toString();
}
3. 输入流转字节数组
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. 输入流转字符串
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) {
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("\\.");
for (int i = 0; i < ips.length; 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();
}
if(!validIP(strIp)) return null;
int[] ip = new int[4];
String[] ips
= strIp.
split("\\.");
return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
}