Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

UUID to byte array (and back)

843790Jun 11 2008 — edited Jun 11 2008
I'm transforming a UUID in a byte array to send it using a socket using the code above:
UUID id = UUID.randomUUID();
String cleanString = id.toString().replaceAll("-", "");
byte[] bytes = cleanString.getBytes();
to get it back on the other side i use:
String fullString = new String(bytes);
fullString = fullString.substring(0, 8) + "-" + fullString.substring(8, 12) + "-" + fullString.substring(12, 16) + "-" + fullString.substring(16, 20) + "-" + fullString.substring(20, 32);
UUID id = UUID.fromString(fullString);
Well, that's works fine except by the fact that it uses 32 bytes (256 bits) insted the 16 bytes (128 bits) that is the real UUID size, and i really need to save bits.
That's clear that the problem is to use toString, but i couldn't fine any way to convert it to a 16 bytes array.

any idea?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 9 2008
Added on Jun 11 2008
4 comments
2,380 views