字符串转byte数组
public static byte[] stringtobyte(String string) {
byte[] bytes = string.getBytes();
for (int i = bytes.length - 1; i >= 0; i--) {
bytes[i] -= (byte) '0';
}
return bytes;
}
byte数组转Hex字符串
public static String bytes2HexString(byte[] b, int len) {
String ret = "";
for (int i = 0; i < len; i++) {
String hex = Integer.toHexString(b[ i ] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
}
合并byte数组
/**
* 合并数组
*
* @param firstArray 第一个数组
* @param secondArray 第二个数组
* @return 合并后的数组
*/
public static byte[] concat(byte[] firstArray, byte[] secondArray) {
if (firstArray == null || secondArray == null) {
if (firstArray != null)
return firstArray;
if (secondArray != null)
return secondArray;
return null;
}
byte[] bytes = new byte[firstArray.length + secondArray.length];
System.arraycopy(firstArray, 0, bytes, 0, firstArray.length); //是一种本地静态方法,用于将元素从源数组复制到目标数组。
System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length);
return bytes;
}