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.
26 lines
579 B
26 lines
579 B
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();
|
|
}
|
|
}
|