将 ByteArray 转换为 UUID java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24408984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:02:51  来源:igfitidea点击:

Convert ByteArray to UUID java

javabytearrayuuid

提问by Android

Question is How do I convert ByteArray to GUID.

问题是如何将 ByteArray 转换为 GUID。

Previously I converted my guid to byte array, and after some transaction I need my guid back from byte array. How do I do that. Although irrelevant but conversion from Guid to byte[] is as below

以前我将我的 guid 转换为字节数组,在一些交易之后我需要从字节数组中取回我的 guid。我怎么做。虽然无关,但从 Guid 到 byte[] 的转换如下

    public static byte[] getByteArrayFromGuid(String str)
    {
        UUID uuid = UUID.fromString(str);
        ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
        bb.putLong(uuid.getMostSignificantBits());
        bb.putLong(uuid.getLeastSignificantBits());

        return bb.array();
    }

but how do I convert it back??

但我如何将其转换回来?

I tried this method but its not returning me same value

我试过这个方法,但它没有给我返回相同的值

    public static String getGuidFromByteArray(byte[] bytes)
    {
        UUID uuid = UUID.nameUUIDFromBytes(bytes);
        return uuid.toString();
    }

Any help will be appreciated.

任何帮助将不胜感激。

采纳答案by Aaron Digulla

The method nameUUIDFromBytes()converts a name into a UUID. Internally, it applied hashing and some black magic to turn any name (i.e. a string) into a valid UUID.

该方法nameUUIDFromBytes()将名称转换为 UUID。在内部,它应用散列和一些黑魔法将任何名称(即字符串)转换为有效的 UUID。

You must use the new UUID(long, long);constructor instead:

您必须改用new UUID(long, long);构造函数:

public static String getGuidFromByteArray(byte[] bytes) {
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    long high = bb.getLong();
    long low = bb.getLong();
    UUID uuid = new UUID(high, low);
    return uuid.toString();
}

But since you don't need the UUID object, you can just do a hex dump:

但是因为你不需要 UUID 对象,你可以做一个十六进制转储:

public static String getGuidFromByteArray(byte[] bytes) {
    StringBuilder buffer = new StringBuilder();
    for(int i=0; i<bytes.length; i++) {
        buffer.append(String.format("%02x", bytes[i]));
    }
    return buffer.toString();
}

回答by haraldK

Try:

尝试:

public static String getGuidFromByteArray(byte[] bytes) {
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    UUID uuid = new UUID(bb.getLong(), bb.getLong());
    return uuid.toString();
}

Your problem is that UUID.nameUUIDFromBytes(...)only creates type 3 UUIDs, but you want any UUID type.

您的问题是UUID.nameUUIDFromBytes(...)只创建类型 3 UUID,但您想要任何 UUID 类型。

回答by Brett Okken

Try doing same process in reverse:

尝试反向执行相同的过程:

public static String getGuidFromByteArray(byte[] bytes)
{
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    UUID uuid = new UUID(bb.getLong(), bb.getLong());
    return uuid.toString();
}

For both building and parsing your byte[], you really need to consider the byte order.

对于构建和解析您的 byte[],您确实需要考虑字节顺序

回答by fabiolimace

There's a method in UuidUtilfrom uuid-creatorthat does that.

还有一个方法,UuidUtiluuid-creator那个做到这一点。

UUID uuid = UuidUtil.fromBytesToUuid(bytes);

Another method does the reverse.

另一种方法则相反。

byte[] bytes = UuidUtil.fromUuidToBytes(uuid);

https://github.com/f4b6a3/uuid-creator

https://github.com/f4b6a3/uuid-creator