52 lines
1.2 KiB
Java
52 lines
1.2 KiB
Java
package ZeusServer.Helpers;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
public class Bytes {
|
|
|
|
// Long
|
|
|
|
public static byte[] longToBytes(long x) {
|
|
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
|
buffer.putLong(x);
|
|
return buffer.array();
|
|
}
|
|
|
|
public static long bytesToLong(byte[] bytes) {
|
|
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
|
buffer.put(bytes);
|
|
buffer.flip();
|
|
return buffer.getLong();
|
|
}
|
|
|
|
// Integer
|
|
|
|
public static byte[] intToBytes(int x) {
|
|
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
|
|
buffer.putInt(x);
|
|
return buffer.array();
|
|
}
|
|
|
|
public static int bytesToInt(byte[] bytes) {
|
|
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
|
|
buffer.put(bytes);
|
|
buffer.flip();
|
|
return buffer.getInt();
|
|
}
|
|
|
|
// Short
|
|
|
|
public static byte[] shortToBytes(short x) {
|
|
ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);
|
|
buffer.putShort(x);
|
|
return buffer.array();
|
|
}
|
|
|
|
public static short bytesToShort(byte[] bytes) {
|
|
ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);
|
|
buffer.put(bytes);
|
|
buffer.flip();
|
|
return buffer.getShort();
|
|
}
|
|
}
|