4

I am converting a Guid to Base64 in C# using the following code:

var id = Guid.Parse("be9f1bb6-5c8e-407d-85a3-d5ef31f21b4d");
var base64=Convert.ToBase64String(id.ToByteArray());

Output

thufvo5cfUCFo9XvMfIbTQ==

When I try to do the same in Java using the following:

java.util.Base64.Encoder encoder=Base64.getEncoder();
UUID uuid = UUID.fromString("be9f1bb6-5c8e-407d-85a3-d5ef31f21b4d");
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
encoder.encodeToString(bb.array());

Different output

vp8btlyOQH2Fo9XvMfIbTQ==

What I am doing wrong in my Java code? How can I get the same result I am getting using C#?

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
Yahya Hussein
  • 8,767
  • 15
  • 58
  • 114
  • Why do you need to parse it into a UID first? – ernest_k Jul 31 '18 at 09:19
  • tried to convert it from string, I got totally different result – Yahya Hussein Jul 31 '18 at 09:20
  • Why should Guid.parse() and UUID.fromString() generate the same result? Is this defined somewhere? – Konrad Jul 31 '18 at 09:20
  • ```bb.array()``` and ```id.ToByteArray()``` are not the same. – zhh Jul 31 '18 at 09:27
  • 1
    Guid of C# and UUID of Java is not same! [This](https://stackoverflow.com/questions/5745512/how-to-read-a-net-guid-into-a-java-uuid) might help you! – Zico Jul 31 '18 at 09:49
  • Also additional backgroud gives an [answer](https://stackoverflow.com/a/17310081/283828) to this question: [Is there any difference between a GUID and a UUID?](https://stackoverflow.com/questions/246930/is-there-any-difference-between-a-guid-and-a-uuid) – LuCio Jul 31 '18 at 10:00

1 Answers1

3

The structure is a bit different, but swapping some bytes in the first part of the byte array fixes your problem.

java.util.Base64.Encoder encoder= Base64.getEncoder();
UUID uuid = UUID.fromString("be9f1bb6-5c8e-407d-85a3-d5ef31f21b4d");
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());

byte[] uuid_bytes = bb.array();
byte[] guid_bytes = Arrays.copyOf(uuid_bytes,uuid_bytes.length);

guid_bytes[0] = uuid_bytes[3];
guid_bytes[1] = uuid_bytes[2];
guid_bytes[2] = uuid_bytes[1];
guid_bytes[3] = uuid_bytes[0];
guid_bytes[4] = uuid_bytes[5];
guid_bytes[5] = uuid_bytes[4];
guid_bytes[6] = uuid_bytes[7];
guid_bytes[7] = uuid_bytes[6];

String result = encoder.encodeToString(guid_bytes);
Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
Terje
  • 1,753
  • 10
  • 13
  • It seems that .NET uses a non-standard bit order. Maybe it will be helpful for somebody: https://stackoverflow.com/q/10190817/632199 and https://stackoverflow.com/a/16722909/632199 – Denis Dec 20 '19 at 12:49
  • See also https://en.wikipedia.org/wiki/Universally_unique_identifier#Encoding "Many systems encode the UUID entirely in a big-endian format." "Other systems, notably Microsoft's marshalling of UUIDs in their COM/OLE libraries, use a mixed-endian format, whereby the first three components of the UUID are little-endian, and the last two are big-endian." – Denis Dec 20 '19 at 13:06