GUID to ByteArray
27,482
Solution 1
I would rely on built in functionality:
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return bb.array();
or something like,
ByteArrayOutputStream ba = new ByteArrayOutputStream(16);
DataOutputStream da = new DataOutputStream(ba);
da.writeLong(uuid.getMostSignificantBits());
da.writeLong(uuid.getLeastSignificantBits());
return ba.toByteArray();
(Note, untested code!)
Solution 2
public static byte[] newUUID() {
UUID uuid = UUID.randomUUID();
long hi = uuid.getMostSignificantBits();
long lo = uuid.getLeastSignificantBits();
return ByteBuffer.allocate(16).putLong(hi).putLong(lo).array();
}
Solution 3
You can check UUID
from apache-commons. You may not want to use it, but check the sources to see how its getRawBytes()
method is implemented:
public UUID(long mostSignificant, long leastSignificant) {
rawBytes = Bytes.append(Bytes.toBytes(mostSignificant), Bytes.toBytes(leastSignificant));
}
Related videos on Youtube
Author by
Chris Dutrow
Creator, EnterpriseJazz.com Owner, SharpDetail.com LinkedIn
Updated on July 09, 2022Comments
-
Chris Dutrow almost 2 years
I just wrote this code to convert a GUID into a byte array. Can anyone shoot any holes in it or suggest something better?
public static byte[] getGuidAsByteArray(){ UUID uuid = UUID.randomUUID(); long longOne = uuid.getMostSignificantBits(); long longTwo = uuid.getLeastSignificantBits(); return new byte[] { (byte)(longOne >>> 56), (byte)(longOne >>> 48), (byte)(longOne >>> 40), (byte)(longOne >>> 32), (byte)(longOne >>> 24), (byte)(longOne >>> 16), (byte)(longOne >>> 8), (byte) longOne, (byte)(longTwo >>> 56), (byte)(longTwo >>> 48), (byte)(longTwo >>> 40), (byte)(longTwo >>> 32), (byte)(longTwo >>> 24), (byte)(longTwo >>> 16), (byte)(longTwo >>> 8), (byte) longTwo }; }
In C++, I remember being able to do this, but I guess theres no way to do it in Java with the memory management and all?:
UUID uuid = UUID.randomUUID(); long[] longArray = new long[2]; longArray[0] = uuid.getMostSignificantBits(); longArray[1] = uuid.getLeastSignificantBits(); byte[] byteArray = (byte[])longArray; return byteArray;
Edit
If you want to generate a completely random UUID as bytes that does not conform to any of the official types, this will work and wastes 10 fewer bits than type 4 UUIDs generated by UUID.randomUUID():
public static byte[] getUuidAsBytes(){ int size = 16; byte[] bytes = new byte[size]; new Random().nextBytes(bytes); return bytes; }
-
Gishu almost 14 yearsDupe - stackoverflow.com/questions/1055413 ?
-
Chris Dutrow almost 14 yearsNo, I was mentioning that in C++, you could have skipped having to copy everything into another array, but it doesn't look like you can do that in Java.
-
Saurabh almost 14 yearspossible duplicate of Sending a Java UUID to C++ as bytes and back over TCP
-
-
Anon21 almost 11 yearsIt does not give the same UUID when I call UUID.nameUUIDFromBytes(bb.array());
-
bcolyn over 10 years@AlexandreH.Tremblay that's to be expected, nameUUIDFromBytes takes a hash from the bytes you pass to it. to deserialize the format above read the longs again and use the constructor UUID(long,long)
-
MoienGK about 9 yearssorry for commenting on old post. but why not use this : uuid.toString().getBytes()
-
MoienGK about 9 yearsand how you should convert the byte[] to mostSignificat and leastSignificatnt in order to regenerate the original uuid?
-
Patrick Linskey over 7 years
uuid.toString().getBytes()
builds a byte[] containing the byte representation of the system's default string encoding of the textual representation of the UUID. So, probably the UTF-8 bytes that represent a string like37B4C0FB-6BEB-4EEF-8D4F-5552C3C76952
. That's 36 bytes long. On the other hand, the proposed solution is just twolong
s, or 128 bits, or 16 bytes. So, 44% of the space.