C# Tips

C# Tip Article

How to convert GUID to Integer

GUID is 128 bit (16 bytes) data. To convert GUID to integer without data loss, we cannot use Int32 or Int64.

.NET 4.0 introduced BigInteger struct which is 128 bit integer. And GUID can be easily converted to BigInteger as follows.

using System;
using System.Numerics;

class Program
	static void Main(string[] args)
		// ex: 8a847645-8cac-422c-962a-fdf3aa220065            
		Guid g = Guid.NewGuid();

		// Convert GUID to BigInteger
		// ex: 134252730720501571475137903438348973637
		BigInteger bigInt = new BigInteger(g.ToByteArray());