Skip to content

Instantly share code, notes, and snippets.

@noeticpenguin
Created September 16, 2013 19:51
Show Gist options
  • Save noeticpenguin/6585674 to your computer and use it in GitHub Desktop.
Save noeticpenguin/6585674 to your computer and use it in GitHub Desktop.
GuidUtil
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