public class StringExUtils {
private final static String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
/**
* 根据传输的长度 往字符串前补0
*
* @param data 字符串
* @param length 总长度
* @return
*/
public static String checkData(String data, int length) {
while (data.length() < length) {
data = "0" + data;
}
return data;
}
/**
* 根据长度,前补0后,按每两个通过空格进行一组分割
*
* @param str
* @return
*/
public static String alternate(String str) {
char[] cx = str.toCharArray();
str = "";
for (int i = 0; i < cx.length; i += 2) {
str += cx[i];
str += cx[i + 1];
str += " ";
}
return str;
}
/**
* 给字符串每个字符前加3
*/
public static String plusCharacter(String str) {
String str1 = "";
for (int i = 0; i < str.length(); i++) {
str1 += "3" + str.substring(i, i + 1);
}
return str1;
}
/**
* byte数组转16进制字串
*
* @param b 字节数组
* @return 16进制字串
*/
public static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
/**
* 16进制字符串转byte数组
*
* @param inHex 待转换的Hex字符串
* @return 转换后的byte数组结果
*/
public static byte[] hexToByteArray(String inHex) {
int hexlen = inHex.length();
byte[] result;
if (hexlen % 2 == 1) {
//奇数
hexlen++;
result = new byte[(hexlen / 2)];
inHex = "0" + inHex;
} else {
//偶数
result = new byte[(hexlen / 2)];
}
int j = 0;
for (int i = 0; i < hexlen; i += 2) {
result[j] = hexToByte(inHex.substring(i, i + 2));
j++;
}
return result;
}
/**
* Hex字符串转byte
*
* @param inHex 待转换的Hex字符串
* @return 转换后的byte
*/
public static byte hexToByte(String inHex) {
return (byte) Integer.parseInt(inHex, 16);
}
/**
* byte转String
*
* @param b
* @return
*/
public static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return HEX_DIGITS[d1] + HEX_DIGITS[d2];
}
/**
* String转16进制
*
* @param str
* @return
*/
public static String StringToHex16String(String str) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder builder = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
builder.append(chars[bit]);
bit = bs[i] & 0x0f;
builder.append(chars[bit]);
builder.append(' ');
}
return builder.toString().trim();
}
/**
* 16进制转String
*
* @param hexStr
* @return
*/
public static byte[] hexStr2Str(String hexStr) {
String str = "0123456789ABCDEF";
char[] hexs = hexStr.toCharArray();
byte[] bytes = new byte[hexStr.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++) {
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return bytes;
}
/**
* MD5编码
*
* @param origin
* @return
*/
public static String MD5Encode(String origin) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
resultString = byteArrayToHexString(md.digest(resultString
.getBytes()));
} catch (Exception e) {
// log.error("", e);
}
return resultString;
}
// Base64加密
public static String getBase64(String str) {
byte[] b = null;
String s = null;
try {
b = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
//log.error("", e);
}
if (b != null) {
s = new BASE64Encoder().encode(b);
}
return s;
}
// Base64解密
public static String getFromBase64(String s) {
byte[] b = null;
String result = null;
if (s != null) {
BASE64Decoder decoder = new BASE64Decoder();
try {
b = decoder.decodeBuffer(s);
result = new String(b, "utf-8");
} catch (Exception e) {
//log.error("", e);
}
}
return result;
}
/**
* 从,号分割字串中查找是否存在目标字串
*
* @param srcstr
* @param aimstr
* @return
*/
public static boolean checkExist(String srcstr, String aimstr) {
if (srcstr == null)
return false;
StringTokenizer st = new StringTokenizer(srcstr, ",");
while (st.hasMoreTokens()) {
if (st.nextToken().equals(aimstr)) {
return true;
}
}
return false;
}
/**
* 转码
*
* @param str
* @return
*/
public static String gb2iso(String str) {
if (str == null) {
return "";
}
String result = "";
try {
byte[] tmp = str.getBytes("GBK");
result = new String(tmp, "ISO8859_1");
} catch (UnsupportedEncodingException e) {
//log.error("convert gb2iso error: ", e);
}
return result;
}
/**
* 转码
*
* @param str
* @return
*/
public static String gb2utf(String str) {
if (str == null) {
return "";
}
String result = "";
try {
byte[] tmp = str.getBytes("GBK");
result = new String(tmp, "UTF-8");
} catch (UnsupportedEncodingException e) {
//log.error("convert gb2utf error: ", e);
}
return result;
}
/**
* 转码
*
* @param str
* @return
*/
public static String iso2gb(String str) {
if (str == null) {
return "";
}
String result = "";
try {
byte[] tmp = str.getBytes("ISO8859_1");
result = new String(tmp, "GBK");
} catch (UnsupportedEncodingException e) {
//log.error("convert iso2gb error: ", e);
}
return result;
}
/**
* 转码
*
* @param str
* @return
*/
public static String iso2utf(String str) {
if (str == null) {
return "";
}
String result = "";
try {
byte[] tmp = str.getBytes("ISO8859_1");
result = new String(tmp, "UTF-8");
} catch (UnsupportedEncodingException e) {
//log.error("convert iso2utf error: ", e);
}
return result;
}
/**
* 转码
*
* @param str
* @return
*/
public static String utf2gb(String str) {
if (str == null) {
return "";
}
String result = "";
try {
byte[] tmp = str.getBytes("UTF-8");
result = new String(tmp, "GBK");
} catch (UnsupportedEncodingException e) {
//log.error("convert utf2gb error: ", e);
}
return result;
}
/**
* 转码
*
* @param str
* @return
*/
public static String utf2iso(String str) {
if (str == null) {
return "";
}
String result = "";
try {
byte[] tmp = str.getBytes("UTF-8");
result = new String(tmp, "ISO8859_1");
} catch (UnsupportedEncodingException e) {
//.error("convert utf2iso error: ", e);
}
return result;
}
/**
* @功能: BCD码转为10进制串(阿拉伯数据)
* @参数: BCD码
* @结果: 10进制串
*/
public static String bcd2Str(byte[] bytes) {
StringBuffer temp = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
temp.append((byte) (bytes[i] & 0x0f));
}
return temp.toString();
}
/**
* @功能: 10进制串转为BCD码
* @参数: 10进制串
* @结果: BCD码
*/
public static byte[] str2Bcd(String asc) {
int len = asc.length();
int mod = len % 2;
if (mod != 0) {
asc = "0" + asc;
len = asc.length();
}
byte abt[] = new byte[len];
if (len >= 2) {
len = len / 2;
}
byte bbt[] = new byte[len];
abt = asc.getBytes();
int j, k;
for (int p = 0; p < asc.length() / 2; p++) {
if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
j = abt[2 * p] - '0';
} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
j = abt[2 * p] - 'a' + 0x0a;
} else {
j = abt[2 * p] - 'A' + 0x0a;
}
if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
k = abt[2 * p + 1] - '0';
} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
k = abt[2 * p + 1] - 'a' + 0x0a;
} else {
k = abt[2 * p + 1] - 'A' + 0x0a;
}
int a = (j << 4) + k;
byte b = (byte) a;
bbt[p] = b;
}
return bbt;
}
/**
* 转码
*
* @param value
* @return
*/
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) {
sbu.append((int) chars[i]).append(",");
} else {
sbu.append((int) chars[i]);
}
}
return sbu.toString();
}
/**
* 转码 STRING为radix进制
*/
public static String asciiToString(String value, int radix) {
StringBuffer sbu = new StringBuffer();
String[] chars = value.split(",");
for (int i = 0; i < chars.length; i++) {
sbu.append((char) Integer.parseInt(chars[i], radix));
}
return sbu.toString();
}
/**
* ascii转String
*/
public static String asciiToString(String value)
{
StringBuffer stringBuffer = new StringBuffer();
String[] chars = value.split(",");
for(String c : chars){
stringBuffer.append((char) Integer.parseInt(c));
}
return stringBuffer.toString();
}
}