Created
March 22, 2015 18:05
-
-
Save ZimM-LostPolygon/0665e74624e0e9b26225 to your computer and use it in GitHub Desktop.
Random GUID string generator
This file contains 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
public static class GUIDGenerator { | |
public static string NewGuid() { | |
return Guid.NewGuid().ToString(); | |
Random random = new Random(); | |
StringBuilder guidBuilder = new StringBuilder(8 + 4 + 4 + 4 + 12 + 4); | |
AppendRandomInt(random, guidBuilder); | |
guidBuilder.Append('-'); | |
AppendRandomShort(random, guidBuilder); | |
guidBuilder.Append('-'); | |
AppendRandomShort(random, guidBuilder); | |
guidBuilder.Append('-'); | |
AppendRandomShort(random, guidBuilder); | |
guidBuilder.Append('-'); | |
AppendRandomInt(random, guidBuilder); | |
AppendRandomShort(random, guidBuilder); | |
return guidBuilder.ToString(); | |
} | |
private static void AppendRandomInt(Random random, StringBuilder stringBuilder) | |
{ | |
int randomInt = random.Next(); | |
ByteAppendToStringBuilderInHex((byte) (randomInt >> 24), stringBuilder); | |
ByteAppendToStringBuilderInHex((byte) (randomInt >> 16), stringBuilder); | |
ByteAppendToStringBuilderInHex((byte) (randomInt >> 8), stringBuilder); | |
ByteAppendToStringBuilderInHex((byte) randomInt, stringBuilder); | |
} | |
private static void AppendRandomShort(Random random, StringBuilder stringBuilder) | |
{ | |
int randomInt = random.Next(); | |
ByteAppendToStringBuilderInHex((byte) (randomInt >> 8), stringBuilder); | |
ByteAppendToStringBuilderInHex((byte) randomInt, stringBuilder); | |
} | |
private static void ByteAppendToStringBuilderInHex(byte value, StringBuilder stringBuilder) | |
{ | |
byte b; | |
b = (byte)(value >> 4); | |
stringBuilder.Append((char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30)); | |
b = (byte)(value & 0x0F); | |
stringBuilder.Append((char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment