Skip to content

Instantly share code, notes, and snippets.

@davidmc24
Created October 6, 2015 16:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save davidmc24/0588900f3200eba3ea80 to your computer and use it in GitHub Desktop.
Save davidmc24/0588900f3200eba3ea80 to your computer and use it in GitHub Desktop.
Convert Microsoft GUID to Java UUID
// Original source: https://github.com/zagyi/adsync4j/blob/master/core/src/main/java/org/adsync4j/impl/UUIDUtils.java
/*******************************************************************************
* ADSync4J (https://github.com/zagyi/adsync4j)
*
* Copyright (c) 2013 Balazs Zagyvai
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Balazs Zagyvai
******************************************************************************/
package org.adsync4j.impl;
import java.util.UUID;
/**
* Utility class dealing with the {@link UUID}s.
*/
public class UUIDUtils {
/**
* Converts a byte array into an {@link UUID} object.
* <p/>
* Microsoft stores GUIDs in a binary format that differs from the RFC standard of UUIDs (RFC #4122). (See details
* at http://en.wikipedia.org/wiki/Globally_unique_identifier) This function takes a byte array read from Active
* Directory and correctly decodes it as a {@link UUID} object.
*
* @param bytes Byte array received as en entry attribute from Active Directory.
* @return {@link UUID} object created from the byte array, or null in case the passed array is not exactly 16 bytes long.
*/
public static UUID bytesToUUID(byte[] bytes) {
if (bytes != null && bytes.length == 16) {
long msb = bytes[3] & 0xFF;
msb = msb << 8 | (bytes[2] & 0xFF);
msb = msb << 8 | (bytes[1] & 0xFF);
msb = msb << 8 | (bytes[0] & 0xFF);
msb = msb << 8 | (bytes[5] & 0xFF);
msb = msb << 8 | (bytes[4] & 0xFF);
msb = msb << 8 | (bytes[7] & 0xFF);
msb = msb << 8 | (bytes[6] & 0xFF);
long lsb = bytes[8] & 0xFF;
lsb = lsb << 8 | (bytes[9] & 0xFF);
lsb = lsb << 8 | (bytes[10] & 0xFF);
lsb = lsb << 8 | (bytes[11] & 0xFF);
lsb = lsb << 8 | (bytes[12] & 0xFF);
lsb = lsb << 8 | (bytes[13] & 0xFF);
lsb = lsb << 8 | (bytes[14] & 0xFF);
lsb = lsb << 8 | (bytes[15] & 0xFF);
return new UUID(msb, lsb);
}
return null;
}
}
@robotdan
Copy link

Thanks! This was very helpful.

Here is a version using ByteBuffer if anyone is interested.

// MSB bytes are the first 8 bytes in this order [3,2,1,0,5,4,7,6]
long msb = ByteBuffer.allocate(8)
                     .put(3, bytes[0])
                     .put(2, bytes[1])
                     .put(1, bytes[2])
                     .put(0, bytes[3])
                     .put(5, bytes[4])
                     .put(4, bytes[5])
                     .put(7, bytes[6])
                     .put(6, bytes[7])
                     .getLong();

// LSB are just the last 8 bytes in the same order
long lsb = ByteBuffer.wrap(bytes, 8, 8).getLong();

return new UUID(msb, lsb);

@davidmc24
Copy link
Author

Thanks, robotdan. Glad it was helpful!

@piratecarrot
Copy link

piratecarrot commented May 12, 2023

This is to read a GUID direct from a buffer

private static UUID getGuid(ByteBuffer buffer) {
        if (buffer.remaining() >= 16) {
            long msb = buffer.get(buffer.position() + 3) & 0xFF;
            msb = msb << 8 | (buffer.get(buffer.position() + 2) & 0xff);
            msb = msb << 8 | (buffer.get(buffer.position() + 1) & 0xff);
            msb = msb << 8 | (buffer.get(buffer.position()) & 0xff);

            msb = msb << 8 | (buffer.get(buffer.position() + 5) & 0xff);
            msb = msb << 8 | (buffer.get(buffer.position() + 4) & 0xff);

            msb = msb << 8 | (buffer.get(buffer.position() + 7) & 0xff);
            msb = msb << 8 | (buffer.get(buffer.position() + 6) & 0xff);

            long lsb = buffer.get(buffer.position() + 8) & 0xff;
            lsb = lsb << 8 | (buffer.get(buffer.position() + 9) & 0xff);
            lsb = lsb << 8 | (buffer.get(buffer.position() + 10) & 0xff);
            lsb = lsb << 8 | (buffer.get(buffer.position() + 11) & 0xff);
            lsb = lsb << 8 | (buffer.get(buffer.position() + 12) & 0xff);
            lsb = lsb << 8 | (buffer.get(buffer.position() + 13) & 0xff);
            lsb = lsb << 8 | (buffer.get(buffer.position() + 14) & 0xff);
            lsb = lsb << 8 | (buffer.get(buffer.position() + 15) & 0xff);

            buffer.position(buffer.position() + 16);
            return new UUID(msb, lsb);
        }

        return null;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment