[@小川游鱼][¥20]Java 中怎样将 bytes 转换为 long 类型?
这样是否题目要求的呢?
public class ByteUtil {
private static final long HEX_LONG = 0x0000000000000000L;
private static final byte HEX_BYTE = (byte) 0xff;
public static Long byteToLong(Byte[] bytes) {
if (bytes == null || bytes.length != 8) {
throw new RuntimeException('param error.');
}
long result = HEX_LONG;
for (int i = 0; i = 0; i--) {
sb.append((x & 0x0000000000000001) == 1l ? '1' : '0');
x >>= 1;
}
return sb.reverse().toString();
}
public static Byte[] getBytesFromLong(Long x) {
Byte[] bytes = new Byte[8];
for (int i = 0; i >= 8;
}
return bytes;
}
public static void main(String[] args) {
System.out.println(byteToLong(getBytesFromLong(1236786534l)));
System.out.println(byteToLong(getBytesFromLong(-1236786534l)));
System.out.println(byteToLong(getBytesFromLong(-0l)));
System.out.println(byteToLong(getBytesFromLong(0l)));
// System.out.println('Long.maxvalue=' + Long.MAX_VALUE + ',byteToLong='
// + byteToLong(
// new Byte[] { HEX_BYTE, HEX_BYTE, HEX_BYTE, HEX_BYTE, HEX_BYTE,
// HEX_BYTE, HEX_BYTE, HEX_BYTE, }));
}
}
赞0
踩0