简体   繁体   English

将UUID转换为GUID,反之亦然

[英]Convert a UUID into a GUID and vice versa

I have this UUID which is changed inside a hqlQuery result when querying a MSSQL database: 查询MSSQL数据库时,我在hqlQuery结果中更改了这个UUID:

createQuery("... where UUID = '917f116e-3447-4524-a959-ece721413f93'").list() //finds the right object

but returns an object with a modified UUID 6e117f91-4734-2445-a959-ece721413f93 但会返回带有修改的UUID 6e117f91-4734-2445-a959-ece721413f93

917f116e-3447-4524-a959-ece721413f93 // correct UUID goes into query
6e117f91-4734-2445-a959-ece721413f93 // modified UUID from the resultset

now when i do a query with the modified UUID: 现在,当我使用修改后的UUID进行查询时:

createQuery("... where UUID = '6e117f91-4734-2445-a959-ece721413f93'").list() 

i don't find an object. 我找不到对象。

So i need to convert the modified one back to the original (and also the other way around). 因此,我需要将修改后的一个转换回原始格式(以及其他方法)。 I don't know if this is an little vs big endian issue. 我不知道这是否是大端序问题。

I make no claim this is the best solution, but this is a solution. 我没有断言这是最好的解决方案,但这是一个解决方案。 This should encode big endian into little endian and vice versa. 这应该将大端编码为小端,反之亦然。

import java.util.UUID;

// ...

 public static UUID uuidMarshalIntoMSFormat(UUID uuid)
  {
    if (uuid == null)
    {
      throw new IllegalArgumentException("Parameter, uuid cannot be null");
    }

    final long msbBigEndian = uuid.getMostSignificantBits();
    final long msbLittleEndian =
      ((msbBigEndian & 0x0000_00FF_0000_0000L) << 24) |
        ((msbBigEndian & 0x0000_FF00_0000_0000L) << 8) |
        ((msbBigEndian & 0x00FF_0000_0000_0000L) >>> 8) |
        ((msbBigEndian & 0xFF00_0000_0000_0000L) >>> 24) |
        ((msbBigEndian & 0x0000_0000_FF00_0000L) >>> 8) |
        ((msbBigEndian & 0x0000_0000_00FF_0000L) << 8) |
        ((msbBigEndian & 0x0000_0000_0000_FF00L) >>> 8) |
        ((msbBigEndian & 0x0000_0000_0000_00FFL) << 8);
    return new UUID(msbLittleEndian, uuid.getLeastSignificantBits());
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM