Created
September 16, 2013 19:51
-
-
Save noeticpenguin/6585674 to your computer and use it in GitHub Desktop.
GuidUtil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global with sharing class GuidUtil { | |
private static String kHexChars = '0123456789abcdef'; | |
global static String NewGuid() { | |
String returnValue = ''; | |
Integer nextByte = 0; | |
for (Integer i=0; i<16; i++) { | |
if (i==4 || i==6 || i==8 || i==10) | |
returnValue += '-'; | |
nextByte = (Math.round(Math.random() * 255)-128) & 255; | |
if (i==6) { | |
nextByte = nextByte & 15; | |
nextByte = nextByte | (4 << 4); | |
} | |
if (i==8) { | |
nextByte = nextByte & 63; | |
nextByte = nextByte | 128; | |
} | |
returnValue += getCharAtIndex(kHexChars, nextByte >> 4); | |
returnValue += getCharAtIndex(kHexChars, nextByte & 15); | |
} | |
return returnValue; | |
} | |
global static String getCharAtIndex(String str, Integer index) { | |
if (str == null) return null; | |
if (str.length() <= 0) return str; | |
if (index == str.length()) return null; | |
return str.substring(index, index+1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment