// 声明:此乃原创,转载注明author
//author:胡名海
//// 从字节数组到十六进制字符串转换
public static String Bytes2HexString(byte[] b) {
if(b==null){
return null;
}
byte[] buff = new byte[2 * b.length];//[2 * b.length]
for (int i = 0; i < b.length; i++) {
buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
buff[2 * i + 1] = hex[b[i] & 0x0f];
}
return new String(buff);
}
public static String Bytes2HexString(byte[] b) {
if(b==null){
return null;
}
byte[] buff = new byte[2 * b.length];//[2 * b.length]
for (int i = 0; i < b.length; i++) {
buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
buff[2 * i + 1] = hex[b[i] & 0x0f];
}
return new String(buff);
}
// 从十六进制字符串到字节数组转换
public static byte[] HexString2Bytes(String hexstr) {
byte[] b = new byte[hexstr.length() / 2];
int j = 0;
for (int i = 0; i < b.length; i++) {
char c0 = hexstr.charAt(j++);
char c1 = hexstr.charAt(j++);
b[i] = (byte) ((parse(c0) << 4) | parse(c1));
}
return b;
}
/**public static byte[] HexString2Bytes(String hexstr) {
byte[] b = new byte[hexstr.length() / 2];
int j = 0;
for (int i = 0; i < b.length; i++) {
char c0 = hexstr.charAt(j++);
char c1 = hexstr.charAt(j++);
b[i] = (byte) ((parse(c0) << 4) | parse(c1));
}
return b;
}
* 把字节转换成UTF-8字符串
*
* @param source
* @return
*/
public static String getUTF8StringFromBytes(byte source[]) {
String dst = "";
try {
dst = (new String(source, "UTF-8"));
} catch (UnsupportedEncodingException e) {
dst = "";
}
return dst;
}