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));
}
Share:
27,482

Related videos on Youtube

Chris Dutrow
Author by

Chris Dutrow

Creator, EnterpriseJazz.com Owner, SharpDetail.com LinkedIn

Updated on July 09, 2022

Comments

  • Chris Dutrow
    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;
    }
    
  • Anon21
    Anon21 almost 11 years
    It does not give the same UUID when I call UUID.nameUUIDFromBytes(bb.array());
  • bcolyn
    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
    MoienGK about 9 years
    sorry for commenting on old post. but why not use this : uuid.toString().getBytes()
  • MoienGK
    MoienGK about 9 years
    and how you should convert the byte[] to mostSignificat and leastSignificatnt in order to regenerate the original uuid?
  • Patrick Linskey
    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 like 37B4C0FB-6BEB-4EEF-8D4F-5552C3C76952. That's 36 bytes long. On the other hand, the proposed solution is just two longs, or 128 bits, or 16 bytes. So, 44% of the space.