tags:

views:

175

answers:

5

Hi,

I want to combine two guid values and generate a 32 bit alphanumberic value(It can be done by using hashing).

A: 

Depends on the platform and details of what you are trying to do.

In .NET/C# you could jus take avery simple approach:

var result = g1.GetHashCode() ^ g2.GetHashCode();
Richard
XOR would prolly be better :)
cwap
Don't you mean GetHashCode() ?
Preet Sangha
Yes and yes... will fix.
Richard
A: 

Why not try a simple operator i.e. AND, OR, XOR etc. To combine the two. XOR would be your best bet hear I would imagine as it has the nice property of when xoring the result with either of the two inputs you will get the other.

Edit: having just looked at this solution, there is a problem with it. The values would have to be normalised. Take a look at Vinay's Answer for a better solution.

Jamie Lewis
+2  A: 

You can't convert 2 128-bit GUIDs into a 16-bit or 32-bit value and maintain uniqueness. For your stated application (use value in URL) this doesn't seem to make sense, as a given value in the URL could map to any number of GUID combinations. Have you considered this?

The best approach would be to use an URL-shortening lookup where you generate a unique ID and map it to the GUIDs if needed - similarly to bit.ly or tinyurl.com.

Vinay Sajip
A: 

Assuming you want to generate a 32 byte value you can just concatenate the GUIDs since they are 16 byte each. If you really need a 32 bit value the only solution I see is generating your own 32 bit values and storing the related GUIDs in a database so you can retrieve them later.

Simon Groenewolt
A: 

Not Pretty, but it works..

 private static Guid MungeTwoGuids(Guid guid1, Guid guid2)
 {
     const int BYTECOUNT = 16;
     byte[] destByte = new byte[BYTECOUNT];
     byte[] guid1Byte = guid1.ToByteArray();
     byte[] guid2Byte = guid2.ToByteArray();

     for (int i = 0; i < BYTECOUNT; i++)
     {
         destByte[i] = (byte) (guid1Byte[i] ^ guid2Byte[i]);
     }
      return new Guid(destByte);
 }

and yes, I can deal with the non-unique-guarantee in my case

Paul