我正在从Java连接到Active Directory服务器。我添加属性:
env.put("java.naming.ldap.attributes.binary", "objectGUID");
然后我像这样阅读 objecUUID:
Attribute a = result.getAttributes().get("objectUUID");
byte[] b = (byte[])a.get();
并像这样格式化:
String id = Hex.
encodeHexString(b).
replaceAll(
"(.{8})(.{4})(.{4})(.{4})(.{12})",
"$1-$2-$3-$4-$5"
)
);
结果是一个格式很好的 UUID。当我想通过其UUID查找条目时,我删除了破折号:
id = id.replaceAll("[^a-zA-Z0-9]+", "");
然后插入反斜杠:
id = id.replaceAll("([a-zA-Z0-9]{2,2})", "\\$1");
这一切都很好用。我遇到的问题是Apache Directory Studio将(例如(我的用户的UUID显示为:
8e591e3a-35ab-45cc-8dca-c5e451adc975
我的代码显示相同条目的 UUID:
3a1e598e-ab35-cc45-8dca-c5e451adc975
如您所见,高阶字节和低阶字节交换为左侧的八个字节,但在右侧相同。为什么?这似乎很奇怪...
。.rm
如果你看这里:
https://en.wikipedia.org/wiki/Universally_unique_identifier
Name Length (Bytes) Length (Hex Digits) Contents
time_low 4 8 integer giving the low 32 bits of the time
time_mid 2 4 integer giving the middle 16 bits of the time
time_hi_and_version 2 4 4-bit "version" in the most significant bits, followed by the high 12 bits of the time
clock_seq_hi_and_res clock_seq_low 2 4 1-3 bit "variant" in the most significant bits, followed by the 13-15 bit clock sequence
node 6 12 the 48-bit node id
您可以看到前 3 段是与"时间"数据相关的整数/短整型,因此具有字节序,而其他部分只是二进制数据,因此没有。