public static byte[] unlong2H4bytes(long n) {
byte[] b = new byte[4];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
b[2] = (byte) (n >> 16 & 0xff);
b[3] = (byte) (n >> 24 & 0xff);
return b;
}
public static String byte2Hex(byte[] bytes) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xff & bytes[i]);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
执行
int startTimeSecond = 1629359452;
byte[] bytes2 = unlong2H4bytes(startTimeSecond);
String s1 = byte2Hex(bytes2);
log.info("{}", s1);
输出
5c0d1e61
|