Note that there are some explanatory texts on larger screens.

plurals
  1. POGUID to ByteArray
    primarykey
    data
    text
    <p>I just wrote this code to convert a GUID into a byte array. Can anyone shoot any holes in it or suggest something better?</p> <pre><code> public static byte[] getGuidAsByteArray(){ UUID uuid = UUID.randomUUID(); long longOne = uuid.getMostSignificantBits(); long longTwo = uuid.getLeastSignificantBits(); return new byte[] { (byte)(longOne &gt;&gt;&gt; 56), (byte)(longOne &gt;&gt;&gt; 48), (byte)(longOne &gt;&gt;&gt; 40), (byte)(longOne &gt;&gt;&gt; 32), (byte)(longOne &gt;&gt;&gt; 24), (byte)(longOne &gt;&gt;&gt; 16), (byte)(longOne &gt;&gt;&gt; 8), (byte) longOne, (byte)(longTwo &gt;&gt;&gt; 56), (byte)(longTwo &gt;&gt;&gt; 48), (byte)(longTwo &gt;&gt;&gt; 40), (byte)(longTwo &gt;&gt;&gt; 32), (byte)(longTwo &gt;&gt;&gt; 24), (byte)(longTwo &gt;&gt;&gt; 16), (byte)(longTwo &gt;&gt;&gt; 8), (byte) longTwo }; } </code></pre> <p>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?:</p> <pre><code> UUID uuid = UUID.randomUUID(); long[] longArray = new long[2]; longArray[0] = uuid.getMostSignificantBits(); longArray[1] = uuid.getLeastSignificantBits(); byte[] byteArray = (byte[])longArray; return byteArray; </code></pre> <h2>Edit</h2> <p>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 <a href="http://en.wikipedia.org/wiki/Universally_Unique_Identifier">10 fewer bits</a> than type 4 UUIDs generated by UUID.randomUUID():</p> <pre><code> public static byte[] getUuidAsBytes(){ int size = 16; byte[] bytes = new byte[size]; new Random().nextBytes(bytes); return bytes; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload