How to convert GUID to Integer in C#
By FoxLearn 11/10/2024 11:32:35 AM 120
Converting a GUID to an integer in C# isn't a straightforward process because a GUID is a 128-bit value, while an integer in C# is only 32-bits.
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.
For example:
// ex: 6172d469-3e9e-4234-bc53-b90f77212d24 Guid g = Guid.NewGuid(); Console.WriteLine(g); // Convert GUID to BigInteger // ex: 48086539959425620223526443617323504745 BigInteger bigInt = new BigInteger(g.ToByteArray()); Console.WriteLine(bigInt);
g.ToByteArray()
converts the GUID to a 16-byte array.
- How to set Filter Criteria with multi conditions in C#?
- How to get application folder path in C#
- How to copy data to clipboard in C#
- How to mark a method as obsolete or deprecated in C#
- How to Call the Base Constructor in C#
- Deep Copy of Object in C#
- How to Catch Multiple Exceptions in C#
- How to cast int to enum in C#
Categories
Popular Posts
SB Admin Template
11/17/2024
RuangAdmin Template
11/17/2024
DASHMIN Admin Dashboard Template
11/17/2024
K-WD Tailwind CSS Admin Dashboard Template
11/17/2024