Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a .NET Guid into a Java UUID

Tags:

I need to communicate a Guid that was generated in .NET to a Java application. I use Guid.ToByteArray() to store it on disk as a byte[], 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 byte[]:

private UUID(byte[] data) {     long msb = 0;     long lsb = 0;     assert data.length == 16;     for (int i=0; i<8; i++)         msb = (msb << 8) | (data[i] & 0xff);     for (int i=8; i<16; i++)         lsb = (lsb << 8) | (data[i] & 0xff);     this.mostSigBits = msb;     this.leastSigBits = lsb; } 

However, when I inspect the UUID using toString(), the Java UUID is different from the .NET Guid.

For example, the .NET Guid

888794c2-65ce-4de1-aa15-75a11342bc63 

turns into the Java UUID

c2948788-ce65-e14d-aa15-75a11342bc63 

It seems that the byte ordering of the first three groups is reversed, while the ordering in the last two groups is the same.

Since I would expect the toString() 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?

Edit: To clarify, the implementation is not my own. It is the private constructor of the java.util.UUID class that takes a byte[], which I copied to use for the purpose of reading a byte[] from disk into a UUID.

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.

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 always generating these bytes in the same order?

like image 910
The Slicer Avatar asked Apr 21 '11 14:04

The Slicer


People also ask

Can we convert string to UUID in Java?

Class StringToUUIDConverterConverts from a String to a java. util. UUID by calling UUID. fromString(String) .

What is a GUID Java?

Overview. UUID (Universally Unique Identifier), also known as GUID (Globally Unique Identifier) represents a 128-bit long value that is unique for all practical purposes.

Is Java a UUID?

Java UUID Class A UUID represents a 128-bit long value that is unique to all practical purpose. It is used to identify information in the computer system. The UUID class belongs to java.

What is UUID number in Java?

A UUID represents a 128-bit value. It is used for for creating random file names, session id in web application, transaction id etc. There are four different basic types of UUIDs: time-based, DCE security, name-based, and randomly generated UUIDs.


1 Answers

Could you not just store the .Net Guid as a string and read it into Java? That way you don't need to worry about byte order or anything.

If not then This explains how the bytes are laid out in C#

http://msdn.microsoft.com/en-us/library/fx22893a.aspx

like image 177
Russell Troywest Avatar answered Nov 03 '22 16:11

Russell Troywest