Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.