title: SQL Server Java Compatible GUID / UUID (Big Endian UniqueIdentifier)
body: <p>I am trying to convert a 128 bit binary to a uniqueidentifier in sql that is the same as in .net and java.</p>
  
  <p>I know java uses big endians, so I would like to make that the base.</p>
  
  <p>I can get the correct endianess in .net, but am really struggling with it in SQL Server.</p>
  
  <p>Java:</p>
  
  <pre><code>        byte[] bytesOfMessage = "google.com".getBytes("UTF-8");
  
          MessageDigest md = MessageDigest.getInstance("MD5");
          byte[] md5 = md.digest(bytesOfMessage);
          ByteBuffer bb = ByteBuffer.wrap(md5);
          LongBuffer ig = bb.asLongBuffer();
          return new UUID(ig.get(0), ig.get(1));
  </code></pre>
  
  <p>returns 1d5920f4-b44b-27a8-02bd-77c4f0536f5a</p>
  
  <p>.Net</p>
  
  <pre><code>    System.Security.Cryptography.MD5 c = System.Security.Cryptography.MD5.Create();
      byte[] b = c.ComputeHash(Encoding.UTF8.GetBytes("google.com"));
      int z = System.Net.IPAddress.HostToNetworkOrder(BitConverter.ToInt32(b, 0));
      short y = System.Net.IPAddress.HostToNetworkOrder(BitConverter.ToInt16(b, 4));
      short x = System.Net.IPAddress.HostToNetworkOrder(BitConverter.ToInt16(b, 6));
      Guid g = new Guid(z, y, x, b.Skip(8).ToArray());
      return g;
  </code></pre>
  
  <p>returns 1d5920f4-b44b-27a8-02bd-77c4f0536f5a</p>
  
  <p>SQL</p>
  
  <pre><code>DECLARE @s VARCHAR(MAX) = 'google.com' --'goolge.com'
  
  DECLARE @md5 BINARY(16) = HASHBYTES
          (
              'MD5',
              @s
          )
  
  DECLARE @a BINARY(4) = 
          CONVERT
          (
              BINARY(4),
              REVERSE
              (
                  CONVERT
                  (
                      BINARY(4),
                      LEFT(@md5, 4)
                  )
              )
          )
  
  DECLARE @b BINARY(2) = 
          CONVERT
          (
              BINARY(2),
              REVERSE
              (
                  CONVERT
                  (
                      BINARY(2),
                      RIGHT(@md5, 12)
                  )
              )
          )
  
  DECLARE @c BINARY(2) = 
          CONVERT
          (
              BINARY(2),
              REVERSE
              (
                  CONVERT
                  (
                      BINARY(2),
                      RIGHT(@md5, 10)
                  )
              )
          )
  
  DECLARE @d BINARY(8) = 
          CONVERT
          (
              BINARY(8),
              RIGHT(@md5, 8)
          )
  
  SELECT  
      CONVERT
      (
          UNIQUEIDENTIFIER,
          @a + @b + @c + @d
      )
  </code></pre>
  
  <p>returns D86B5A7F-7A25-4895-A6D0-63BA3A706627</p>
  
  <p>I am able to get all three to produce the same value when converting to an int64, but the GUID is baffling me.</p>
  
  <p><a href="https://stackoverflow.com/questions/15579946/sql-bigint-hash-to-match-c-sharp-int64-hash">Original Issue</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8467072/sql-server-varbinary-bigint-with-bitconverter-toint64-values-are-different">Original Answer</a></p>