You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.2 KiB
55 lines
1.2 KiB
5 years ago
|
package ${domain}.frame;
|
||
|
|
||
|
/**
|
||
|
* BytesUtil - 字节数组工具类
|
||
|
*
|
||
|
* @author wangbing
|
||
|
* @version 0.0.1
|
||
|
* @since 2017-01-01
|
||
|
*/
|
||
|
public class BytesUtil {
|
||
|
|
||
|
private static final char[] HEXES = {
|
||
|
'0', '1', '2', '3',
|
||
|
'4', '5', '6', '7',
|
||
|
'8', '9', 'a', 'b',
|
||
|
'c', 'd', 'e', 'f'
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* byte数组转16进制字符串
|
||
|
*/
|
||
|
public static String bytes2Hex(byte[] bytes) {
|
||
|
if (bytes == null || bytes.length == 0) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
StringBuilder hex = new StringBuilder();
|
||
|
|
||
|
for (byte b : bytes) {
|
||
|
hex.append(HEXES[(b >> 4) & 0x0F]);
|
||
|
hex.append(HEXES[b & 0x0F]);
|
||
|
}
|
||
|
|
||
|
return hex.toString();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 16进制字符串转byte数组
|
||
|
*/
|
||
|
public static byte[] hex2Bytes(String hex) {
|
||
|
if (hex == null || hex.length() == 0) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
char[] hexChars = hex.toCharArray();
|
||
|
byte[] bytes = new byte[hexChars.length / 2];
|
||
|
|
||
|
for (int i = 0; i < bytes.length; i++) {
|
||
|
bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
|
||
|
}
|
||
|
|
||
|
return bytes;
|
||
|
}
|
||
|
}
|