2014-06-25 15 views
9

La domanda è Come convertire ByteArray in GUID.Converti ByteArray in UUID java

Precedentemente ho convertito il mio guid in array di byte, e dopo alcune transazioni ho bisogno del mio guid back dall'array di byte. Come lo faccio. Sebbene irrilevante, ma conversione da Guid a byte [] è come sotto

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

ma come posso riconvertire?

ho provato questo metodo, ma la sua non mi tornare stesso valore

public static String getGuidFromByteArray(byte[] bytes) 
    { 
     UUID uuid = UUID.nameUUIDFromBytes(bytes); 
     return uuid.toString(); 
    } 

Qualsiasi aiuto sarà apprezzato.

risposta

17

Il metodo nameUUIDFromBytes() converte un nome in un UUID. Internamente, applicava l'hashing e un po 'di magia nera per trasformare qualsiasi nome (cioè una stringa) in un UUID valido.

è necessario utilizzare la funzione di costruzione new UUID(long, long); invece:

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

Ma dal momento che non è necessario l'oggetto UUID, si può solo fare un dump esadecimale:

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

+1 per Spiegazione: _Specially_ per Hashing e alcuni _black magic_ :) –

+1

Per essere più specifici sulla "magia nera" (che non chiarisce realmente cosa sta realmente accadendo), dai un'occhiata a http: // docs. oracle.com/javase/7/docs/api/java/util/UUID.html. Vale a dire, 'UUID pubblico (long mostSigBits, long leastSigBits)' può costruire qualsiasi tipo di UUID che si desidera. Probabilmente può persino rendere UUID non validi con tipi diversi da 1, 2, 3 o 4 - ma non l'ho verificato. 'nameUUIDFromBytes' costruirà solo un UUID di tipo 3. –

7

Prova:

public static String getGuidFromByteArray(byte[] bytes) { 
    ByteBuffer bb = ByteBuffer.wrap(bytes); 
    UUID uuid = new UUID(bb.getLong(), bb.getLong()); 
    return uuid.toString(); 
} 

tuo problema è che UUID.nameUUIDFromBytes(...) crea solo digitare 3 UUID, ma si vuole qualsiasi tipo UUID.

+0

Se si tratta di 8 o 16 byte, funzionerà. Cosa sarebbe successo se avesse 4 byte. Penso che si bloccherà (java.nio.bufferoverflowexception). come possiamo fissare questo? – mindus

+1

@mindus Un UUID è [definito per avere 128 bit (16 byte)] (http://en.wikipedia.org/wiki/Universally_unique_identifier#Definition). Se hai solo i primi 32 bit, non c'è modo di "aggiustarlo". Si potrebbe voler aggiungere la convalida dell'input e lanciare un 'IllegalArgumentException' se l'input è! = 16 byte però. – haraldK

1

provare a fare lo stesso processo in senso inverso:

public static String getGuidFromByteArray(byte[] bytes) 
{ 
    ByteBuffer bb = ByteBuffer.wrap(bytes); 
    UUID uuid = new UUID(bb.getLong(), bb.getLong()); 
    return uuid.toString(); 
} 

Per entrambi costruzione e l'analisi vostra byte [], si ha realmente bisogno di prendere in considerazione il byte order.

+0

Perfetto. Funziona per me – Android

+0

Se è 8 o 16 byte, funzionerà. Cosa sarebbe successo se avesse 4 byte. Penso che si bloccherà (java.nio.bufferoverflowexception). come possiamo fissare questo? – mindus