总结平时遇到的各种字符串,byte,hex之间的相互转换
/*
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
*/
public static String hexStr2Str(String bytes) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(
bytes.length() / 2);
String hexString = "0123456789ABCDEF";
// 将每2位16进制整数组装成一个字节
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
.indexOf(bytes.charAt(i + 1))));
return new String(baos.toByteArray());
}
/**
* 将字符串转化成内存中存放的16进制表示 eg. "31323334" ==> "1234"
*
* @param src
* @return
*/
public static String hexByteStringToStr(String src) {
if (src.length() % 2 != 0) {
return null;
}
byte[] tempData = str2bytes(src);
String finalStr = "";
for (int i = 0; i < tempData.length; i++) {
char tempChar = (char) tempData[i];
finalStr += tempChar;
}
return finalStr;
}
/**
* 将16进制组成的字符串转换成byte数组 例如 hex2Byte("0710BE8716FB"); 将返回一个byte数组
* b[0]=0x07;b[1]=0x10;...b[5]=0xFB;
*
* @param src 待转换的16进制字符串
* @return
*/
public static byte[] str2bytes(String src) {
if (src == null || src.length() == 0 || src.length() % 2 != 0) {
return null;
}
int nSrcLen = src.length();
byte byteArrayResult[] = new byte[nSrcLen / 2];
StringBuffer strBufTemp = new StringBuffer(src);
String strTemp;
int i = 0;
while (i < strBufTemp.length() - 1) {
strTemp = src.substring(i, i + 2);
byteArrayResult[i / 2] = (byte) Integer.parseInt(strTemp, 16);
i += 2;
}
return byteArrayResult;
}
/**
* 将byte数组转换成16进制组成的字符串 例如 一个byte数组 b[0]=0x07;b[1]=0x10;...b[5]=0xFB; byte2hex(b);
* 将返回一个字符串"0710BE8716FB"
*
* @param bytes 待转换的byte数组
* @return
*/
public static String bytesToHexString(byte[] bytes) {
if (bytes == null) {
return "";
}
StringBuffer buff = new StringBuffer();
int len = bytes.length;
for (int j = 0; j < len; j++) {
if ((bytes[j] & 0xff) < 16) {
buff.append('0');
}
buff.append(Integer.toHexString(bytes[j] & 0xff));
}
return buff.toString();
}
/**
* 16进制转10进制
*
* @param strHex
* @return
*/
public static int hexToInt(String strHex) {
int nResult = 0;
if (!isHex(strHex))
return nResult;
String str = strHex.toUpperCase();
if (str.length() > 2) {
if (str.charAt(0) == '0' && str.charAt(1) == 'X') {
str = str.substring(2);
}
}
int nLen = str.length();
for (int i = 0; i < nLen; ++i) {
char ch = str.charAt(nLen - i - 1);
try {
nResult += (getHex(ch) * getPower(16, i));
} catch (Exception e) {
e.printStackTrace();
}
}
return nResult;
}
/**
* 判断是否是16进制数
*
* @param strHex
* @return
*/
public static boolean isHex(String strHex) {
int i = 0;
if (strHex.length() > 2) {
if (strHex.charAt(0) == '0'
&& (strHex.charAt(1) == 'X' || strHex.charAt(1) == 'x')) {
i = 2;
}
}
for (; i < strHex.length(); ++i) {
char ch = strHex.charAt(i);
if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')
|| (ch >= 'a' && ch <= 'f'))
continue;
return false;
}
return true;
}
/**
* 计算16进制对应的数值
*
* @param ch
* @return
* @throws Exception
*/
private static int getHex(char ch) throws Exception {
if (ch >= '0' && ch <= '9')
return (int) (ch - '0');
if (ch >= 'a' && ch <= 'f')
return (int) (ch - 'a' + 10);
if (ch >= 'A' && ch <= 'F')
return (int) (ch - 'A' + 10);
throw new Exception("error param");
}
/**
* 计算幂
*
* @param nValue
* @param nCount
* @return
* @throws Exception
*/
private static int getPower(int nValue, int nCount) throws Exception {
if (nCount < 0)
throw new Exception("nCount can't small than 1!");
if (nCount == 0)
return 1;
int nSum = 1;
for (int i = 0; i < nCount; ++i) {
nSum = nSum * nValue;
}
return nSum;
}