Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. public class GUIDFactory {
  2.  
  3. private static String hexCharacters = "0123456789abcdef";
  4.  
  5. /**
  6. * Generates a random GUID (hex) based on the following format :
  7. *
  8. * <pre>
  9. * nnnnnnnn-nnnn-nnnn-nnnnnnnnnnnnnnnnn
  10. * </pre>
  11. *
  12. * Example :
  13. *
  14. * <pre>
  15. * 13219ec0-3a81-44c5-a300-de14b7d0235f
  16. * </pre>
  17. *
  18. * @return A global unique identifier as a String
  19. */
  20. public static String makeGUID() {
  21.  
  22. String newGUID = "";
  23. int cmp = 0;
  24.  
  25. for (int i = 0; i < 16; i++) {
  26.  
  27. if (i == 4 || i == 6 || i == 8 || i == 10)
  28. newGUID += '-';
  29.  
  30. cmp = (int) (Math.round(Math.random() * 255) - 128) & 255;
  31.  
  32. if (i == 6) {
  33. cmp = cmp & 15;
  34. cmp = cmp | (4 << 4);
  35. }
  36.  
  37. if (i == 8) {
  38. cmp = cmp & 63;
  39. cmp = cmp | 128;
  40. }
  41.  
  42. newGUID += hexCharacters.charAt(cmp >> 4);
  43. newGUID += hexCharacters.charAt(cmp & 15);
  44. }
  45.  
  46. return newGUID;
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement