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
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.
GUIDs to the Rescue The format is a well-defined sequence of 32 hex digits grouped into chunks of 8-4-4-4-12.
GUIDs are bigger than 32-bits, what are you trying to accomplish? You cannot convert that to an int32, because it contains a lot more than 32 bits of data.
A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With