public void TestMethod5() { Guid guid = new Guid("{d3902802-2842-4d50-bc5b-134c27efe5ff}"); Console.WriteLine(guid); byte[] guidBytes = guid.ToByteArray(); byte[] sifted = new byte[guidBytes.Length + 1]; Buffer.BlockCopy(guidBytes, 0, sifted, 0, guidBytes.Length); BigInteger bigInteger = new BigInteger(sifted); Assert.AreEqual(BigInteger.One, bigInteger.Sign); string encoded = BaseConvert.ToBaseString(bigInteger, BaseConvert.Base62); Assert.IsNotNull(encoded); Console.WriteLine(encoded); BigInteger result = BaseConvert.FromBaseString(encoded, BaseConvert.Base62); Assert.IsNotNull(result); byte[] actualBytes = result.ToByteArray(); byte[] bytes = new byte[16]; int count = Math.Min(bytes.Length, actualBytes.Length); Buffer.BlockCopy(actualBytes, 0, bytes, 0, count); Guid actual = new Guid(bytes); Assert.AreEqual(guid, actual); Console.WriteLine(actual); }
/// <summary> /// Encodes the given Guid as an encoded string. /// </summary> /// <param name="guid">The Guid to encode</param> /// <returns>The encoded string.</returns> public static string Encode(Guid guid) { byte[] guidBytes = guid.ToByteArray(); // add extra byte to make number positive byte[] sifted = new byte[guidBytes.Length + 1]; Buffer.BlockCopy(guidBytes, 0, sifted, 0, guidBytes.Length); return(BaseConvert.ToBaseString(sifted, BaseConvert.Base62));; }