fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.io.*;
  4. import java.lang.reflect.*;
  5. import java.net.*;
  6. import java.nio.*;
  7. import java.text.*;
  8. import java.util.*;
  9.  
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. String idStr = "4f5e73f72bb2492b9ffa7b12d55111f3";
  15. System.out.println("Converting Guid string: " + idStr);
  16.  
  17. UUID idUuid = fromGuidString(idStr);
  18. System.out.println("To UUID: " + idUuid);
  19.  
  20. String idStr2 = toGuidString(idUuid);
  21. System.out.println("Back to string: " + idStr2);
  22. }
  23.  
  24. public static void reverse(byte[] bytes) {
  25. if (bytes == null)
  26. return;
  27.  
  28. int i = 0;
  29. int j = bytes.length - 1;
  30. byte hold;
  31. while (j > i) {
  32. hold = bytes[j];
  33. bytes[j] = bytes[i];
  34. bytes[i] = hold;
  35. j--;
  36. i++;
  37. }
  38. }
  39.  
  40. final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
  41.  
  42. public static String toHex(byte[] bytes) {
  43. char[] hexChars = new char[bytes.length * 2];
  44. for ( int j = 0; j < bytes.length; j++ ) {
  45. int v = bytes[j] & 0xFF;
  46. hexChars[j * 2] = hexArray[v >>> 4];
  47. hexChars[j * 2 + 1] = hexArray[v & 0x0F];
  48. }
  49. return new String(hexChars);
  50. }
  51.  
  52. public static byte[] fromHex(String hex) {
  53. int len = hex.length();
  54. byte[] data = new byte[len / 2];
  55. for (int i = 0; i < len; i += 2) {
  56. data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
  57. + Character.digit(hex.charAt(i+1), 16));
  58. }
  59. return data;
  60. }
  61.  
  62. public static String toGuidString(UUID uuid) {
  63. byte[] guidBytes = toGuidBytes(uuid);
  64. String to = toHex(guidBytes);
  65. return to;
  66. }
  67.  
  68. public static UUID fromGuidString(String guid) {
  69. guid = guid.replaceAll("-","");
  70. byte[] guidBytes = fromHex(guid);
  71. UUID to = fromGuidBytes(guidBytes);
  72. return to;
  73. }
  74.  
  75. public static UUID fromGuidBytes(byte[] guidBytes) {
  76. ByteBuffer buf = ByteBuffer.wrap(guidBytes);
  77.  
  78. byte[] first4 = new byte[4];
  79. buf.get(first4);
  80. reverse(first4);
  81.  
  82. byte[] second2 = new byte[2];
  83. buf.get(second2);
  84. reverse(second2);
  85.  
  86. byte[] third2 = new byte[2];
  87. buf.get(third2);
  88. reverse(third2);
  89.  
  90. long lsb = buf.getLong();
  91.  
  92. buf = ByteBuffer.wrap(new byte[8])
  93. .put(first4)
  94. .put(second2)
  95. .put(third2);
  96.  
  97. buf.rewind();
  98. long msb = buf.getLong();
  99.  
  100. return new UUID(msb, lsb);
  101. }
  102.  
  103. public static byte[] toGuidBytes(UUID theUuid) {
  104. ByteBuffer first8 = ByteBuffer.allocate(8);
  105. first8.putLong(theUuid.getMostSignificantBits());
  106. first8.rewind();
  107.  
  108. byte[] first4 = new byte[4];
  109. first8.get(first4);
  110. reverse(first4);
  111.  
  112. byte[] second2 = new byte[2];
  113. first8.get(second2);
  114. reverse(second2);
  115.  
  116. byte[] third2 = new byte[2];
  117. first8.get(third2);
  118. reverse(third2);
  119.  
  120. ByteBuffer converted16 = ByteBuffer.allocate(16)
  121. .put(first4)
  122. .put(second2)
  123. .put(third2);
  124.  
  125. ByteBuffer last8 = ByteBuffer.allocate(8);
  126. last8.putLong(theUuid.getLeastSignificantBits());
  127. last8.rewind();
  128.  
  129. converted16.put(last8);
  130.  
  131. return converted16.array();
  132. }
  133. }
Success #stdin #stdout 0.11s 320512KB
stdin
Standard input is empty
stdout
Converting Guid string: 4f5e73f72bb2492b9ffa7b12d55111f3
To UUID: f7735e4f-b22b-2b49-9ffa-7b12d55111f3
Back to string: 4F5E73F72BB2492B9FFA7B12D55111F3