diff --git a/src/main/java/ByteUtils.java b/src/main/java/ByteUtils.java new file mode 100644 index 0000000..b43c7eb --- /dev/null +++ b/src/main/java/ByteUtils.java @@ -0,0 +1,99 @@ +public class ByteUtils { + public static int fromBytesAsInt(byte[] i) { + + return + (((int) i[3] & 0xFF) << 24) | + (((int) i[2] & 0xFF) << 16) | + (((int) i[1] & 0xFF) << 8) | + (((int) i[0] & 0xFF)); + } + + public static long fromBytesAsLong(byte[] i) { + + + return + (((long) i[0] & 0xFF) << 56) | + (((long) i[1] & 0xFF) << 48) | + (((long) i[2] & 0xFF) << 40) | + (((long) i[3] & 0xFF) << 32) | + (((long) i[4] & 0xFF) << 24) | + (((long) i[5] & 0xFF) << 16) | + (((long) i[6] & 0xFF) << 8) | + (((long) i[7] & 0xFF)); + } + + public static long fromBytesAsLongMost(byte[] i) { + + return + (((long) i[0] & 0xFF) << 56) | + (((long) i[1] & 0xFF) << 48) | + (((long) i[2] & 0xFF) << 40) | + (((long) i[3] & 0xFF) << 32) | + (((long) i[4] & 0xFF) << 24) | + (((long) i[5] & 0xFF) << 16) | + (((long) i[6] & 0xFF) << 8) | + (((long) i[7] & 0xFF)); + } + + public static long fromBytesAsLongLast(byte[] i) { + + return + (((long) i[8] & 0xFF) << 56) | + (((long) i[9] & 0xFF) << 48) | + (((long) i[10] & 0xFF) << 40) | + (((long) i[11] & 0xFF) << 32) | + (((long) i[12] & 0xFF) << 24) | + (((long) i[13] & 0xFF) << 16) | + (((long) i[14] & 0xFF) << 8) | + (((long) i[15] & 0xFF)); + } + + public static byte[] toBytes(int i) { + byte[] result = new byte[4]; + + result[0] = (byte) (i >> 24); + result[1] = (byte) (i >> 16); + result[2] = (byte) (i >> 8); + result[3] = (byte) (i /*>> 0*/); + + return result; + } + + public static byte[] toBytes(long i) { + byte[] result = new byte[8]; + + result[0] = (byte) (i >> 56); + result[1] = (byte) (i >> 48); + result[2] = (byte) (i >> 40); + result[3] = (byte) (i >> 32); + result[4] = (byte) (i >> 24); + result[5] = (byte) (i >> 16); + result[6] = (byte) (i >> 8); + result[7] = (byte) (i /*>> 0*/); + + return result; + } + + public static byte[] toBytes(long most, long last) { + byte[] result = new byte[16]; + + result[0] = (byte) (most >> 56); + result[1] = (byte) (most >> 48); + result[2] = (byte) (most >> 40); + result[3] = (byte) (most >> 32); + result[4] = (byte) (most >> 24); + result[5] = (byte) (most >> 16); + result[6] = (byte) (most >> 8); + result[7] = (byte) (most /*>> 0*/); + result[8] = (byte) (last >> 56); + result[9] = (byte) (last >> 48); + result[10] = (byte) (last >> 40); + result[11] = (byte) (last >> 32); + result[12] = (byte) (last >> 24); + result[13] = (byte) (last >> 16); + result[14] = (byte) (last >> 8); + result[15] = (byte) (last /*>> 0*/); + + return result; + } +} diff --git a/src/main/java/UUIDUtil.java b/src/main/java/UUIDUtil.java index 552fe42..d659111 100644 --- a/src/main/java/UUIDUtil.java +++ b/src/main/java/UUIDUtil.java @@ -1,5 +1,7 @@ import com.fasterxml.uuid.Generators; +import org.bouncycastle.util.encoders.Hex; + import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.LongBuffer; @@ -223,5 +225,46 @@ public class UUIDUtil { return null; } } + + public static String UUIDToHex(final UUID orgUUID) { + if (orgUUID == null) { + return null; + } + try { + + final long most = orgUUID.getMostSignificantBits(); + final long last = orgUUID.getLeastSignificantBits(); + return Hex.toHexString(ByteUtils.toBytes(most, last)); + } catch (IllegalArgumentException iae) { + return null; + } + } + + + public static UUID HexToUUID(final String hexString) { + if (hexString == null) { + return null; + } + try { + final ByteBuffer sourceBuffer = ByteBuffer.allocateDirect(16); + sourceBuffer.clear(); + sourceBuffer.put(Hex.decode(hexString)); + sourceBuffer.flip(); + return new UUID(sourceBuffer.getLong(), sourceBuffer.getLong()); + } catch (IllegalArgumentException iae) { + return null; + } + } + + + public static void main(String[] args) { + + final UUID uuid = UUIDUtil.generateUUIDRandomly(); + final String generatedId = UUIDUtil.UUIDToHex(uuid); + + System.out.println(generatedId); + + } + }