public static class Program
public string GetConvertedGUID(string strProductCode) {
string strMungedCode = string.Empty;
const int intArraySize = 32;
int[] arrSequence = new int[]{2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 11, 12, 13, 14, 28, 29, 26, 27, 23, 24, 21, 22, 36, 37, 34, 35, 32, 33, 30, 31};
for (int intIndex = 0; intIndex <= (intArraySize - 1); intIndex++)
strMungedCode = (strMungedCode + strProductCode.Substring((arrSequence[intIndex] - 1), 1));
Block1 = strMungedCode.Substring(0, 8);
Block2 = strMungedCode.Substring(8, 8);
Block3 = strMungedCode.Substring(16, 8);
Block4 = strMungedCode.Substring(24, 8);
strPart1 = DecToBase85(Dec1);
strPart2 = DecToBase85(Dec2);
strPart3 = DecToBase85(Dec3);
strPart4 = DecToBase85(Dec4);
return (strPart1.ReverseString()
+ (strPart3 + strPart4.ReverseString())));
public long HextoDec(string strHex)
lngResult = Convert.ToInt64(strHex, 16);
private string DecitoHex(int ddec)
return ddec.ToString("X8");
public string DecToBase85(long strDecimal) {
int strDec = (int)strDecimal;
string Base85 = "!$%&\'()*+,-.0123456789=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{}~";
long mod = strDecimal % 85;
quint1 = Base85.Substring(num, 1);
if (strDecimal > 84 && strDecimal < 7225)
num = (int)(strDecimal / 85) % 85;
quint1 = Base85.Substring(num, 1);
long mod = strDecimal % 85;
quint2 = Base85.Substring(num, 1);
if (((strDecimal > 7224) && (strDecimal < 614125)))
num = (int)(strDecimal / 7225) % 85;
quint1 = Base85.Substring(num, 1);
num = (int)(strDecimal / 85) % 85;
quint2 = Base85.Substring(num, 1);
long mod = strDecimal % 85;
quint3 = Base85.Substring(num, 1);
if (((strDecimal > 614124) && (strDecimal < 52200625)))
num = (int)(strDecimal / 614125) % 85;
quint1 = Base85.Substring(num, 1);
num = (int)(strDecimal / 7225) % 85;
quint2 = Base85.Substring(num, 1);
num = (int)(strDecimal / 85) % 85;
quint3 = Base85.Substring(num, 1);
long mod = strDecimal % 85;
quint4 = Base85.Substring(num, 1);
if ((strDecimal > 52200624))
num = (int)(strDecimal/52200625) % 85;
quint1 = Base85.Substring(num, 1);
num = (int)(strDecimal / 614125) % 85;
quint2 = Base85.Substring(num, 1);
num = (int)(strDecimal / 7225) % 85;
quint3 = Base85.Substring(num, 1);
num = (int)(strDecimal / 85) % 85;
quint4 = Base85.Substring(num, 1);
long mod = strDecimal % 85;
quint5 = Base85.Substring(num, 1);
public string DDtoGUID(string darwin) {
quad1 = darwin.Substring(0, 5);
quad1 = QuadtoHex(quad1);
quad2 = darwin.Substring(5, 5);
quad2 = QuadtoHex(quad2);
quad3 = darwin.Substring(10, 5);
quad3 = QuadtoHex(quad3);
quad4 = darwin.Substring(15, 5);
quad4 = QuadtoHex(quad4);
return ReconstructProductCode(mungedStr);
public string QuadtoHex(string quad) {
Base85 = "!$%&\'()*+,-.0123456789=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{}~";
for (int i = 5; i >= 1; i--)
b85 = ((Base85.IndexOf(chr) + 1) - 1);
public string ReconstructProductCode(string munged) {
const int intArraySize = 32;
int[] arrSequence = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 13, 14, 15, 16, 9, 10, 11, 12, 23, 24, 21, 22, 19, 20, 17, 18, 31, 32, 29, 30, 27, 28, 25, 26};
for (int intIndex = 0; (intIndex
<= (intArraySize - 1)); intIndex++) {
strProductCode = (strProductCode + munged.Substring((arrSequence[intIndex] - 1), 1));
|| (intIndex == 19))))) {
strProductCode = (strProductCode + "-");
return (strProductCode + "}");
public static string ReverseString(this string s)
if (string.IsNullOrEmpty(s))
char[] array = new char[s.Length];
for (int i = s.Length - 1; i >= 0; i--)
return new string(array);
public static void Main()
GUID2DD conv = new GUID2DD();
string code = "w_1^VX!!!!!!!!!MKKSk";
string result = string.Empty;
if ((code.Substring(0, 1) == "{"))
result = conv.GetConvertedGUID(code);
else if ((code.Length == 20))
result = conv.DDtoGUID(code);
result = "Format of string submitted does not match that of a regular GUID or a Darwin Descriptor. Entries must" +
" be of the form {xxxxxxxx-xxxx-xxxx-xxxx-Xxxxxxxxxxxx} or xxxxxxxxxxxxxxxxxxxx (len=20)";
Console.WriteLine(code+" => " + result);