list/src/main/java/util/ByteUtils.java
2018-07-23 09:14:46 +02:00

26 lines
579 B
Java
Executable File

package util;
import java.nio.ByteBuffer;
public class ByteUtils {
/*
* Taken from <a href="https://stackoverflow.com/a/4485196">StackOverflow</a>
*/
public static byte[] longToBytes(long x) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(x);
return buffer.array();
}
/*
* Taken from <a href="https://stackoverflow.com/a/4485196">StackOverflow</a>
*/
public static long bytesToLong(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.put(bytes);
buffer.flip();//need flip
return buffer.getLong();
}
}