Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GUIDFactory {
- private static String hexCharacters = "0123456789abcdef";
- /**
- * Generates a random GUID (hex) based on the following format :
- *
- * <pre>
- * nnnnnnnn-nnnn-nnnn-nnnnnnnnnnnnnnnnn
- * </pre>
- *
- * Example :
- *
- * <pre>
- * 13219ec0-3a81-44c5-a300-de14b7d0235f
- * </pre>
- *
- * @return A global unique identifier as a String
- */
- public static String makeGUID() {
- String newGUID = "";
- int cmp = 0;
- for (int i = 0; i < 16; i++) {
- if (i == 4 || i == 6 || i == 8 || i == 10)
- newGUID += '-';
- cmp = (int) (Math.round(Math.random() * 255) - 128) & 255;
- if (i == 6) {
- cmp = cmp & 15;
- cmp = cmp | (4 << 4);
- }
- if (i == 8) {
- cmp = cmp & 63;
- cmp = cmp | 128;
- }
- newGUID += hexCharacters.charAt(cmp >> 4);
- newGUID += hexCharacters.charAt(cmp & 15);
- }
- return newGUID;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement