Convert Int to Guid

I have to convert Convert Int32 into Guids and this is what I came up with.

public static class IntExtensions
{
    public static Guid ToGuid(this Int32 value)
    {
        if (value >= 0) // if value is positive
            return new Guid(string.Format("00000000-0000-0000-0000-00{0:0000000000}", value));
        else if (value > Int32.MinValue) // if value is negative
            return new Guid(string.Format("00000000-0000-0000-0000-01{0:0000000000}", Math.Abs(value)));
        else //if (value == Int32.MinValue)
            return new Guid("00000000-0000-0000-0000-012147483648");  // Because Abs(-12147483648) generates a stack overflow due to being > 12147483647 (Int32.Max)
    }
}

But it’s somehow ugly. Has anybody a better idea?

Update:

Yes I know the whole thing is ugly but I am a loss of Ideas. The problem is. I am getting data and have to store it into a Table I cannot change. The sending data primary key is a Int and the table primary key I have to store it is a Guid. The problem is I have to understand what object the sender is talking about but can only store it as a Guid.

Update 2:

Okay I see I have to provide more info here. I am a Webservice receiving data and have to pass along data to an Interface I also can not control. So I neither can model the data received nor the (Interface)database where I have to send the data. Additionally, I have somehow have to map these two things in a way so I somehow can update an item. sigh


Here is a simple way to do it:

public static Guid ToGuid(int value)
{
    byte[] bytes = new byte[16];
    BitConverter.GetBytes(value).CopyTo(bytes, 0);
    return new Guid(bytes);
}

You can change where the copy will happen (vary the index from 0 to 12). It really depends on how you want to define this unusual "int to Guid" conversion.


Had the same issues, needed a Int to Guid then back to Int. Old data that used int ID's but the processing function required a Guid. It was alot less code to write extra functions plus DB changes. Just easier to pass the Int iD in Guid form, knowing that it was not going to use this as its final saving Guid. The save is an Insert, so it was getting a new Guid at the end.

Heres's the code from above and an idea from another post about Guids to ints and getting the Int back out.

public static Guid Int2Guid(int value)
{
    byte[] bytes = new byte[16];
    BitConverter.GetBytes(value).CopyTo(bytes, 0);
    return new Guid(bytes);
}

public static int Guid2Int(Guid value)
{
    byte[] b = value.ToByteArray();
    int bint = BitConverter.ToInt32(b, 0);
    return bint;
}

This Guid2Int should only be passed a Guid that came from an Int.


You can take the digits from an int and format them so that they look like a GUID, but that doesn't make the result a GUID. A GUID is basically a 16 byte number that is computed using an algorithm that guarantees that number is unique. You can generate GUIDs all day long on every computer in the world and not get a duplicate (at least that's the theory). A reformatted int is not unique and it's definitely not a GUID.