Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read a .NET Guid into a Java UUID
    text
    copied!<p>I need to communicate a Guid that was generated in .NET to a Java application. I use <code>Guid.ToByteArray()</code> to store it on disk as a <code>byte[]</code>, then read it into Java and convert it to a UUID. For this purpose I copied the implementation of the (private) constructor of UUID that takes a <code>byte[]</code>:</p> <pre><code>private UUID(byte[] data) { long msb = 0; long lsb = 0; assert data.length == 16; for (int i=0; i&lt;8; i++) msb = (msb &lt;&lt; 8) | (data[i] &amp; 0xff); for (int i=8; i&lt;16; i++) lsb = (lsb &lt;&lt; 8) | (data[i] &amp; 0xff); this.mostSigBits = msb; this.leastSigBits = lsb; } </code></pre> <p>However, when I inspect the UUID using <code>toString()</code>, the Java UUID is different from the .NET Guid.</p> <p>For example, the .NET Guid</p> <pre><code>888794c2-65ce-4de1-aa15-75a11342bc63 </code></pre> <p>turns into the Java UUID</p> <pre><code>c2948788-ce65-e14d-aa15-75a11342bc63 </code></pre> <p>It seems that the byte ordering of the first three groups is reversed, while the ordering in the last two groups is the same.</p> <p>Since I would expect the <code>toString()</code> of both the Guid and the UUID to yield the same result, does anyone know how I should correctly read the .NET Guid into a Java UUID?</p> <p><strong>Edit:</strong> To clarify, the implementation is not my own. It is the private constructor of the <code>java.util.UUID</code> class that takes a <code>byte[]</code>, which I copied to use for the purpose of reading a byte[] from disk into a UUID.</p> <p>I do not want to use strings to store the Guids as I'm storing a lot of them and it seems like a waste of space.</p> <p>Russell Troywest's link at least clarifies why the first couple of groups of the Guid come out reversed, while the second half stays in the same order. The question is, can I depend on .NET <strong>always</strong> generating these bytes in the same order?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload