byte字节数组与⼆进制、⼗六进制字符串的转换    /**
* byte数组转换为⼆进制字符串,每个字节以","隔开
**/
public static String byteArrToBinStr(byte[] b) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < b.length; i++) {
result.String(b[i] & 0xff, 2) + ",");
}
String().substring(0, result.length() - 1);
}
/**
* ⼆进制字符串转换为byte数组,每个字节以","隔开
**/
public static byte[] binStrToByteArr(String binStr) {
String[] temp = binStr.split(",");
byte[] b = new byte[temp.length];
for (int i = 0; i < b.length; i++) {
b[i] = Long.valueOf(temp[i], 2).byteValue();
}
return b;
}
/**
* 字节数组转为⼗六进制字符串(1字节对应2个16进制字符)
* @param byteArray
* @return
*/
public static String toHexString(byte[] byteArray) {
if (byteArray == null || byteArray.length < 1)
throw new IllegalArgumentException("this byteArray must not be null or empty");
final StringBuilder hexString = new StringBuilder();
for (int i = 0; i < byteArray.length; i++) {
if ((byteArray[i] & 0xff) < 0x10)//0~F前⾯不零
hexString.append("0");
hexString.HexString(0xFF & byteArray[i]));
}
二进制与十六进制之间的转换
String().toLowerCase();
}
/**
* 字节数组转为⼗六进制字符串Ⅱ(1字节对应2个16进制字符)
* @param byteArray
* @return
*/
private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public static String bytesToHexFun1(byte[] bytes) {
/
/ ⼀个byte为8位,可⽤两个⼗六进制位标识
char[] buf = new char[bytes.length * 2];
int a = 0;
int index = 0;
for(byte b : bytes) { // 使⽤除与取余进⾏转换
if(b < 0) {
a = 256 + b;
} else {
a = b;
}
buf[index++] = HEX_CHAR[a / 16];
buf[index++] = HEX_CHAR[a % 16];
}
return new String(buf);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。