Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        /// <summary>
        /// Decodes the given encoded string to a Guid.
        /// </summary>
        /// <param name="value">The encoded string of a Guid.</param>
        /// <returns>A Guid that was represented by the encoded string.</returns>
        public static Guid Decode(string value)
        {
            BigInteger result = BaseConvert.FromBaseString(value, BaseConvert.Base62);

            byte[] resultBytes = result.ToByteArray();
            byte[] bytes       = new byte[16];

            // size to guid
            int count = Math.Min(bytes.Length, resultBytes.Length);

            Buffer.BlockCopy(resultBytes, 0, bytes, 0, count);

            return(new Guid(bytes));
        }