tags:

views:

119

answers:

3

Given an int, how can you create the same Guid repeatedly?

Edit:
I'm integrating two systems, one uses ints as a primary key, the other recognises an object by it's Guid. Because of this, I need to be able to recreate the same Guid for a given int so that the second system can recognise the entities coming from the first.

+1  A: 

You could convert an int64 to a byte[] and use the constructor overload to do it:

var guid = new Guid(byteArray);

Edit -- oops.. It's based off of 128 bits, not 64.. If you had a custom type, MyBigInt (a 128-bit int), then you could do it the aforementioned way.

Ian P
+5  A: 

If you create a Guid based on an integer it will no longer be a globally unique identifier.

Having said that you can use the constructor that takes an integer and some other parameters:

int a = 5;
Guid guid = new Guid(a, 0, 0, new byte[8]);

Not really advisable...

Mark Byers
Works perfect, The different elements of the Guid allow me to distinguish between different entity types as well. Thanks!
Greg B
+1  A: 

If you want the guid to consist of just the int then:

int x = 5;
string padded = x.ToString().PadLeft(32,'0');
var intGuid = new Guid(padded);

intGuid.ToString(); // 00000000-0000-0000-0000-000000000005
STW