views:

35

answers:

2

Is this even possible? I don't even think it is, but I saw some code that was trying to do it. However, my unit tests showed that it was not working. I did see some similar thoughts:

Just for clarification. The Guid creation is outside of my control; therefore, I can't just store the value in one of the Guid sets.

+3  A: 

A GUID is 16 bytes long. A "long" is often understood as being 64 bits (i.e. 8 bytes) long. You cannot convert between those without losing or providing extra data.

CesarGon
As long as the guid created fits in the the long, it is possible? It's just when it goes over the long's specified size. Correct?
daub815
A GUID can never fit a long, because, as I say, a GUID's size is 16 bytes, and a long's size is 8 bytes. You cannoy happily discard 8 bytes off a GUID, even if they are zeroes! :-)
CesarGon
You are absolutely correct. I wasn't thinking with that comment :)
daub815
A: 

You can using Guid constructor, a simple but not flawless example:

Guid g1;
long g2;

// {00000001-0000-0000-0000-000000000000}
g1 = new Guid(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

// 00000001
g2 = Convert.ToInt64(g1.ToString().Split('-')[0]);

// 1 + 5 = 6
g2 += 5;

// {00000006-0000-0000-0000-000000000000}
g1 = new Guid((uint)g2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

The first set can handle up to 4294967295 (int). If you need more than it then you have to use another set.

Each set is a hex representation of a unsigned integer, so you can play around with the possibilities.

BrunoLM