so I have a 3rd party application that I have to interface with, this application needs the userID from my users table. the problem is that I store my userIDs as GUIDs and the 3rd party app only accepts an integer. so I figure, if there is a way to convert a GUID to an integer then be able to convert it back (as i get communications from their server regarding the userID) then I don't have to recode a too much. any ideas?
6 Answers
An integer uses 32 bits, whereas a GUID is 128 bits - therefore, there's no way to convert a GUID into an integer without losing information, so you can't convert it back.
EDIT: you could perhaps store the GUID into a GUIDs table where you assign each a unique integer ID. That way, you could get the GUID back given the integer ID.

- 12,274
- 8
- 40
- 53
-
2The size of integer is system dependant. – Filip Ekberg Dec 30 '08 at 21:04
-
But then, you limit yourself to 2^32 ids. – LeppyR64 Dec 30 '08 at 21:04
You'll need a store a mapping from your Guid to the 3rd party's integer: either a new "int thirdPartyId" field in your existing user table, or a new table.

- 54,973
- 13
- 116
- 224
-
i was really hoping to not have to modify the dal but as it turns out my gut instinct was right. – Russ Bradberry Dec 30 '08 at 23:08
You would need a 128-bit integer type to hold the equivalent of a GUID. You can use the Guid's ToByteArray() method to get the bytes and the constructor that accepts a byte array to restore the Guid.
You can also you the GetHashCode() method to get an integer, but two different GUIDs may have the same hashcode since there are more possible Guids than 32-bit integers.

- 98,437
- 31
- 224
- 236
A Guid much bigger than an integer, and can't fit in a integer.

- 34,421
- 21
- 109
- 151

- 7,248
- 27
- 27
Add and integer Identity column, that will basically be a second ID column, and give that columnID to the application.

- 61,751
- 23
- 87
- 115
Create a new Table (MyUserMappings) With Two Columns
1) ThirdPartyUserID (INT, NOT NULL, IDENTITY(1,1))
2) MyUserID (GUID, NOTNULL)
Insert All GUID FROM your UserID to This table by doing
INSERT INTO MyUserMappings(MyUserID) SELECT MyGUIDUserID FROM MyUsers
You also need to put a trigger on your MyUsers Table to insert a new row in MyUsersMappings table every time your MyUsers table gets new row.
Now Create a View That shows all the fields from your MyUsers Table and ThirdParty UserID Field from your MyUsermappingsTable.
Now you will not require any DAL changes to your existing application and your original table is unaffected. So your existing application will stay as it is. And your new code can query the view instead of the table to return the Integer ID and all other user info.
Cheers.