Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ByteArray to UUID java

Tags:

Question is How do I convert ByteArray to 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

    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.

like image 473
Android Avatar asked Jun 25 '14 12:06

Android


People also ask

What is ByteArray in Java?

Java ByteArrayOutputStream class is used to write common data into multiple files. In this stream, the data is written into a byte array which can be written to multiple streams later. The ByteArrayOutputStream holds a copy of data and forwards it to multiple streams.

How do you convert Bytearrays to strings?

Convert byte[] to String (text data) toString() to get the string from the bytes; The bytes. toString() only returns the address of the object in memory, NOT converting byte[] to a string ! The correct way to convert byte[] to string is new String(bytes, StandardCharsets. UTF_8) .

What does ByteArray mean?

A byte array is simply an area of memory containing a group of contiguous (side by side) bytes, such that it makes sense to talk about them in order: the first byte, the second byte etc..


2 Answers

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.

You must use the 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:

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(); } 
like image 186
Aaron Digulla Avatar answered Oct 18 '22 11:10

Aaron Digulla


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.

like image 41
Harald K Avatar answered Oct 18 '22 10:10

Harald K