HTML过滤和补齐(五)

本文汇总了一系列实用的Java工具方法,包括字符串转数值类型、位运算、字符串比较、MD5与Base64转换等,旨在提供高效简洁的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* 转换字符串为long
*
* @param s
* @param def
* @return
*/
public static long getLong(String s, long def) {
long i = def;
try {
i = Long.parseLong(s);
} catch (NumberFormatException e) {
// ignore
}
return i;
}

/**
* 转换字符串为long
*
* @param s
* @return
*/
public static long getLong(String s) {
return getLong(s, 0);
}

/**
* 对整数按位取与
*
* @param a
* @param b
* @return
*/
public static int bitAND(int a, int b) {
return a & b;
}

/**
* 对整数按位取或
*
* @param a
* @param b
* @return
*/
public static int bitOR(int a, int b) {
return a | b;
}

/**
* 对整数按位取异或
*
* @param a
* @param b
* @return
*/
public static int bitXOR(int a, int b) {
return a ^ b;
}

/**
* 判断一个字符串是否在字符数组中出现
*
* @param arr
* @param s
* @return
*/
public static boolean inStrings(String[] arr, String s) {
if (arr != null && s != null && arr.length > 0) {
for (int i = 0; i < arr.length; i++) {
if (s.equals(arr[i])) {
return true;
}
}
}
return false;
}

/**
* 转换字符串为Float
*
* @param s
* @param def
* @return
*/
public static float getFloat(String s, float def) {
float i = def;
try {
i = Float.parseFloat(s);
} catch (NumberFormatException e) {
// ignore
}
return i;
}

/**
* 转换字符串为Float
*
* @param s
* @return
*/
public static float getFloat(String s) {
return getFloat(s, 0);
}

/**
* 把MD5生成的专成Base64,会节省8个字节的空间
*
* @param string
* @return
*/
public static String radix4StringToRadix6String(String string) {
return bytesTo6RadixString(radix4StringToBytes(string));
}

/**
* 把base64的专成标准的MD5表示
*
* @param string
* @return
*/
public static String radix6StringToradix4String(String string) {
return bytesTo4RadixString(radix6StringToBytes(string));
}

/**
* 将MD5生成的byte流,转换成标准的字符输出
*
* @param bs
* @return
*/
public static final String bytesTo4RadixString(byte[] bs) {
int l = bs.length;

char[] out = new char[l << 1];

for (int i = 0, j = 0; i < l; i++) {
out[j++] = digits[(0xF0 & bs[i]) >>> 4];
out[j++] = digits[0x0F & bs[i]];
}

return new String(out);
}

/**
* 将MD5转换过的字符输出还原成byte流
*
* @param string
* @return
*/
public static final byte[] radix4StringToBytes(String string) {
byte[] digesta = null;
if (!isEmpty(string)) {
byte[] tmpBytes = string.getBytes();
digesta = new byte[tmpBytes.length / 2];
for (int i = 0; i < tmpBytes.length; i += 2) {
byte b = cb(tmpBytes[i]);
byte b1 = (i + 1 < tmpBytes.length) ? cb(tmpBytes[i + 1]) : 0;
digesta[i / 2] = (byte) (((b << 4) | b1) & 0xFF);
}
}
return digesta;
}

/**
* = base64decode
*
* @param string
* @return
*/
public static final byte[] radix6StringToBytes(String string) {
return Base64.encodeBase64(string.getBytes());
}

/**
* = base64encode
*
* @param digesta
* @return
*/
public static final String bytesTo6RadixString(byte[] digesta) {
return new String(Base64.encodeBase64(digesta));
}

private static final byte cb(byte b) {
if (b < 58) {
b -= 48;
} else if (b > 96) {
b -= 87;
} else {
b = 0;
}
return (byte) (b & 0x0F);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值