简体   繁体   English

将 ByteArray 转换为 UUID java

[英]Convert ByteArray to UUID java

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.以前我将我的 guid 转换为字节数组,在一些交易之后我需要从字节数组中取回我的 guid。 How do I do that.我怎么做。 Although irrelevant but conversion from Guid to byte[] is as below虽然无关,但从 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.任何帮助将不胜感激。

The method nameUUIDFromBytes() converts a name into a UUID.方法nameUUIDFromBytes()将名称转换为 UUID。 Internally, it applied hashing and some black magic to turn any name (ie a string) into a valid UUID.在内部,它应用散列和一些黑魔法将任何名称(即字符串)转换为有效的 UUID。

You must use the new UUID(long, long);您必须使用new UUID(long, long); constructor instead:构造函数代替:

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();
}

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 类型。

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[],您确实需要考虑字节顺序

The codec BinaryCodec can encode UUIDs to byte arrays.编解码器BinaryCodec可以将 UUID 编码为字节数组。

// Convert a UUID into an array of bytes
UuidCodec<byte[]> codec = new BinaryCodec();
byte[] bytes = codec.encode(uuid);
// Convert an array of bytes into a UUID
UuidCodec<byte[]> codec = new BinaryCodec();
UUID uuid = codec.decode(bytes);

See: uuid-creator请参阅: uuid-creator

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

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